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。?根據數據庫中的信息執行比較。 返回值
IfInStr 返回 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 函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学会用taro封装一个组件
- 下一篇: NFS4文件锁机制探秘