前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VBA专题03:InStr函数

VBA专题03:InStr函数

作者头像
fanjy
发布2019-07-19 16:12:19
5.3K0
发布2019-07-19 16:12:19
举报
文章被收录于专栏:完美Excel完美Excel

学习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:获取字符出现的位置

下面的代码返回一个字符在另一个字符中出现的位置:

代码语言:javascript
复制
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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-07-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 完美Excel 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档