當(dāng)前位置:
首頁(yè) >
VBA InStr 函数
發(fā)布時(shí)間:2023/12/13
44
生活家
生活随笔
收集整理的這篇文章主要介紹了
VBA InStr 函数
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
InStr 函數(shù)
函數(shù) (Visual Basic for Applications)
返回一個(gè) **Variant **(Long) 值,指定一個(gè)字符串在另一個(gè)字符串中首次出現(xiàn)的位置。
語法
InStr([start],string1,string2, [compare])
InStr函數(shù)語法有以下參數(shù):
| Part | 說明 |
|---|---|
| start | 可選。設(shè)置每次搜索的起始位置的數(shù)字表達(dá)式。如果忽略,則搜索從第一個(gè)字符位置開始。如果start包含Null,則出現(xiàn)錯(cuò)誤。如果指定了compare,則start參數(shù)是必需的。 |
| string1 | 必需。要搜索的字符串表達(dá)式。 |
| string2 | 必需。搜索到的字符串表達(dá)式。 |
| compare | 可選。指定字符串比較的類型。如果compare為Null,則將發(fā)生錯(cuò)誤。如果省略compare,則Option Compare設(shè)置將決定比較的類型。指定有效的 LCID (LocaleID) 以在比較中使用區(qū)域設(shè)置特定規(guī)則。 |
設(shè)置
compare參數(shù)設(shè)置如下。
| 常量 | 值 | 說明 |
|---|---|---|
| vbUseCompareOption | -1 | 使用Option Compare語句的設(shè)置執(zhí)行比較。 |
| vbBinaryCompare | 0 | 執(zhí)行二進(jìn)制比較。 |
| vbTextCompare | 1 | 執(zhí)行文本比較。 |
| vbDatabaseCompare | 2 | 僅用于 Microsoft Access。根據(jù)數(shù)據(jù)庫(kù)中的信息執(zhí)行比較。 |
返回值
| If | InStr 返回 |
|---|---|
| string1是零長(zhǎng)度 | 0 |
| string1為Null | NULL |
| string2是零長(zhǎng)度 | start |
| string2為Null | NULL |
| 未找到string2 | 0 |
| 在string1中找到string2 | 找到匹配的位置 |
| start>string2 | 0 |
注釋
InStrB函數(shù)適用于包含在字符串中的字節(jié)數(shù)據(jù)。InStrB返回某字符串在其他字符串中首次出現(xiàn)的字節(jié)位置,而不返回其字符位置。
示例
本示例使用InStr函數(shù)來返回某字符串在其他字符串中首次出現(xiàn)的位置。
VB復(fù)制
Dim SearchString, SearchChar, MyPos
SearchString ="XXpXXpXXPXXP" ' String to search in.
SearchChar = "P" ' Search for "P".
' A textual comparison starting at position 4. Returns 6.
MyPos = Instr(4, SearchString, SearchChar, 1)
' A binary comparison starting at position 1. Returns 9.
MyPos = Instr(1, SearchString, SearchChar, 0)
' Comparison is binary by default (last argument is omitted).
MyPos = Instr(SearchString, SearchChar) ' Returns 9.
MyPos = Instr(1, SearchString, "W") ' Returns 0.
另請(qǐng)參閱
總結(jié)
以上是生活随笔為你收集整理的VBA InStr 函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在ASM中移动数据文件
- 下一篇: Mac上Genymotion模拟器无法联