VBA专题03:InStr函数

论坛 期权论坛 期权     
完美Excel   2019-7-14 05:34   2948   0
学习Excel技术,关注微信公众号:
excelperfect

在VBA中,InStr函数是一个非常有用的函数,可用于查找某字符串在另一个字符串中第一次出现的位置。

InStr函数的语法如下图1所示:


图1

其中:
1.参数Start,可选,指定搜索的起始位置。如果省略该参数,则会从String1的第一个字符开始查找。
2.参数String1,必需,被搜索的字符串。
3.参数String2,必需,要搜索的字符串。
4.参数Compare,可选,指定比较模式。默认为vbBinaryCompare模式,即二进制比较,还可以指定为vbTextCompare模式(文本比较,不区分大小写)和vbDatabaseCompare模式(数据库比较,只适用于Access)。如果指定该参数,就要同时指定参数Start。如果省略该参数,比较模式由Option Compare语句值决定。

简单地说,InStr函数就是查找String2在String1中第一次出现的位置。

InStr函数的返回值有以下几种情形:
1.如果String1长度为0,则返回值0。
2.如果String1为Null,则返回值Null。
3.如果String2长度为0,则返回参数Start的值。
4.如果String2为Null,则返回值Null。
5.如果找不到String2,则返回值0。
6.如果在String1中找到了String2,则返回String2被找到的位置。
7.如果参数Start指定的数值大于String2的长度,,则返回值0。

示例1:获取字符出现的位置
下面的代码返回一个字符在另一个字符中出现的位置:
Sub InstrSample1()
    Dim str1 As String
    Dim str2 As String
    Dim iPos As Long

    str1 = "我的微信公众号是完美Excel"
    str2 = "完美Excel"

    iPos = InStr(1, str1,str2)

    Debug.Print str2 &" 出现在 " & str1 & " 的第"& iPos & "个字符."
End Sub

运行结果如下图2所示。


图2

示例2:统计字符串中包含某子字符串的数量
下面的代码统计字符串str1中发现字符串str2的个数:
Sub InstrSample2()
    Dim str1 As String
    Dim str2 As String
    Dim str As String
    Dim iPos As Long
    Dim iCount As Long
   
    str1 ="ABCDABEF"
    str2 = "AB"
    str = str1
   
    iPos = InStr(1, str1,str2)
    Do While (iPos 0)
        iCount = iCount + 1
        str1 = Mid(str1, iPos+ Len(str2))
        iPos = InStr(1, str1,str2)
    Loop
   
    Debug.Print"""" & str & """" & "中共有" &iCount & "个" & "字符串""" & str2 & """"
End Sub

运行结果如下图3所示。


图3

示例3:获取字符出现的多个位置
如果一个字符串在另一个字符串中多次出现,要获取该字符串出现的这些位置值,示例代码如下:
Sub InstrSample3()
    Dim str1 As String
    Dim str2 As String
    Dim str As String
    Dim iPos As Long
    Dim iPos1 As Long
    Dim iPosAll() As Long
    Dim iCount As Long
   
    str1 ="ABCDABEFAB"
    str2 = "AB"
    str = str1
   
    iPos = InStr(1, str1,str2)
    iPos1 = iPos
    Do While (iPos 0)
        iCount = iCount + 1
        ReDim Preserve iPosAll(1 To iCount)
        iPosAll(iCount) =iPos1
        str1 = Mid(str1, iPos+ Len(str2))
        iPos = InStr(1, str1,str2)
        iPos1 = iPos1 +Len(str2) + iPos - 1
    Loop
   
    Debug.Print"""" & str2 & """" & "出现在" &"""" & str & """" & "中的位置:"
    For iCount =LBound(iPosAll) To UBound(iPosAll)
        Debug.Print iPosAll(iCount)
    Next iCount
End Sub

运行结果如下图4所示。


图4

可以将上面的代码转换成一个自定义函数,由用户传递相应的参数,该函数返回由字符位置组成的数组:
Function InstrSample4(str1 As String, str2 As String) As Long()
    Dim iPos As Long
    Dim iPos1 As Long
    Dim iPosAll() As Long
    Dim iCount As Long
      
    iPos = InStr(1, str1,str2)
    iPos1 = iPos
    Do While (iPos 0)
        iCount = iCount + 1
        ReDim Preserve iPosAll(1 To iCount)
        iPosAll(iCount) =iPos1
        str1 = Mid(str1, iPos+ Len(str2))
        iPos = InStr(1, str1,str2)
        iPos1 = iPos1 +Len(str2) + iPos - 1
    Loop
   
    InstrSample4 = iPosAll()
End Function

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:195
帖子:39
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP