VBA InStr 函数
生活随笔
收集整理的這篇文章主要介紹了
VBA InStr 函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
InStr 函數
函數 (Visual Basic for Applications)
返回一個 **Variant **(Long) 值,指定一個字符串在另一個字符串中首次出現的位置。
語法
InStr([start],string1,string2, [compare])
InStr函數語法有以下參數:
| Part | 說明 |
|---|---|
| start | 可選。設置每次搜索的起始位置的數字表達式。如果忽略,則搜索從第一個字符位置開始。如果start包含Null,則出現錯誤。如果指定了compare,則start參數是必需的。 |
| string1 | 必需。要搜索的字符串表達式。 |
| string2 | 必需。搜索到的字符串表達式。 |
| compare | 可選。指定字符串比較的類型。如果compare為Null,則將發生錯誤。如果省略compare,則Option Compare設置將決定比較的類型。指定有效的 LCID (LocaleID) 以在比較中使用區域設置特定規則。 |
設置
compare參數設置如下。
| 常量 | 值 | 說明 |
|---|---|---|
| vbUseCompareOption | -1 | 使用Option Compare語句的設置執行比較。 |
| vbBinaryCompare | 0 | 執行二進制比較。 |
| vbTextCompare | 1 | 執行文本比較。 |
| vbDatabaseCompare | 2 | 僅用于 Microsoft Access。根據數據庫中的信息執行比較。 |
返回值
| If | InStr 返回 |
|---|---|
| string1是零長度 | 0 |
| string1為Null | NULL |
| string2是零長度 | start |
| string2為Null | NULL |
| 未找到string2 | 0 |
| 在string1中找到string2 | 找到匹配的位置 |
| start>string2 | 0 |
注釋
InStrB函數適用于包含在字符串中的字節數據。InStrB返回某字符串在其他字符串中首次出現的字節位置,而不返回其字符位置。
示例
本示例使用InStr函數來返回某字符串在其他字符串中首次出現的位置。
VB復制
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.
另請參閱
總結
以上是生活随笔為你收集整理的VBA InStr 函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在ASM中移动数据文件
- 下一篇: requests下载大文件_11种方法教