|
楼主 |
发表于 2019-9-28 23:15
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub TEST()
- Dim ARX, BRX
- ARX = Split("AA,AB,AA,AC,AA,AD", ",")
- BRX = DiyFilter(ARX, "A", True, 0)
- BRX = DiyFilter(ARX, "A", True, 1)
- CRX = DiyFilter(ARX, "AA", True)
- MsgBox 1
- End Sub
- Function DiyFilter(SourceArray, Match, Optional ByVal Include As Boolean = True, Optional ByVal Compare As Integer = 0) As Variant()
- Rem SourceArray 必需的?要执行搜索的一维字符串数组?
- Rem Match 必需的?要搜索的字符串?
- Rem Include 可选的。Boolean值,表示返回子串包含还是不包含match字符串。
- Rem Include=True Filter返回的是包含Match子字符串的数组子集
- Rem Include=False Filter返回的不包含Match子字符串的数组子集。
- Rem Compare 可选的。数字值,表示所使用的字符串比较类型。相同=0,包含=1
-
- Dim LB, I, X As Long
- LB = LBound(SourceArray)
- ReDim ARX(LB To LB)
- X = LB
- For I = LB To UBound(SourceArray)
- If Compare = 0 Then
- If ("" & SourceArray(I) = Match) = Include Then
- ReDim Preserve ARX(LB To X)
- ARX(X) = SourceArray(I)
- X = X + 1
- End If
- Else
- If (InStr("" & SourceArray(I), Match) > 0) = Include Then
- ReDim Preserve ARX(LB To X)
- ARX(X) = SourceArray(I)
- X = X + 1
- End If
- End If
- Next
- DiyFilter = ARX
- End Function
复制代码 |
|