'*************************************************************************
'**模 块 名:Filter 数组过滤函数精解示例
'**说 明:蓝凤凰设计商城 浴火凤凰-郭卫 | 蓝凤凰-魔灵 | 郭卫-icecept
'**创 建 人:浴火凤凰-郭卫
'**日 期:2015年10月11日 13:38:55
'**修 改 人:浴火凤凰-郭卫
'**日 期:
'**描 述:QQ:493405998 | 微信\旺旺:icecept
'**版 本:V1.0.0 | http://blog.sina.com.cn/icecept
'*************************************************************************
'●用法:传回数组 = Filter(原始数组, 要找的文本, [如果是包含则为TRUE,否则为FALSE.默认为True], [比较方式])
'●语法 Filter(InputStrings, Value[, Include[, Compare]])
'特别注意:Filter函数是按字符串来比较数组及搜索对象的,而且是按是否具有包含关系而不是相等关系来判断的。
Private Sub Command4_Click()
Dim aa As Variant, bb() As String, prt As Variant
aa = Array("蓝凤凰", "浴火凤凰", "魔灵", "郭卫", "icecept", "龙浩晨", "圣采儿", "周维青", "上官冰儿", "上官菲儿", "上官雪儿", "巫月寒", "东方寒月")
bb = Filter(aa, Text1.Text, False) '显示不包含文本框输入内容的所有项目
For Each prt In bb
Debug.Print prt,
Next
Debug.Print
End Sub
Private Sub Command5_Click()
Dim Arr
Arr = Array(1, 2, 3, 4, 5, 6, 11, 13, 15, 11, 2, 3, 5)
Debug.Print "原数组:" & Join(Arr, ",") & vbCrLf
'true 显示包含1的项,False显示不包含1的项,如果省略此项默认为true
x = Filter(Arr, 1, False)
Debug.Print "不包含1的数组:" & Join(x, ",")
'原数组:1,2,3,4,5,6,11,13,15,11,2,3,5
'
'不包含1的数组:2,3,4,5,6,2,3,5
End Sub
Private Sub Form_Load()
Me.Show
Dim x(2)
x(0) = "kjwang"
x(1) = "wangkj"
x(2) = "peter"
'第三项不写默认为True,True 显示包含有kj的项 Flase 显示不包含kj的项
v = Filter(x, "kj")
Print v(0), v(1)
'vbTextCompare 忽略大小写,默认(vbTextCompare)是区分大小写
v = Filter(x, "kj", False)
Print v(0)
End Sub
Private Sub Command2_Click()
Dim aa(10) As String
Dim bb() As String
Dim i As Integer
For i = 0 To 9
If i Mod 2 = 0 Then
aa(i) = "aa" & Str$(i)
Else
aa(i) = "bb" & Str$(i)
End If
Next i
'True 显示数组中包含aa的项
bb = Filter(aa, "aa", True)
'Flase 显示数组中不包含aa的项
' bb = Filter(aa, "aa", False)
Debug.Print Join(bb, ",")
End Sub
Private Sub Command1_Click()
Dim Arr
Arr = Array(1, 2, 3, 4, 5, 6, 21, 13, 15, 12, 25, 3, 5)
Debug.Print "原数组:" & Join(Arr, ",") & vbCrLf
'true 显示包含2的项,False显示不包含2的项,如果省略此项默认为true
x = Filter(Arr, 2, True)
Debug.Print "包含2的数组:" & Join(x, ",")
'原数组:1,2,3,4,5,6,21,13,15,12,25,3,5
'
'包含2的数组:2,21,12,25
End Sub
Private Sub Command3_Click()
Dim b() As String
Dim a(60) As String
Dim i As Integer
For i = 0 To 60
a(i) = CStr(i)
Next i
Debug.Print "原数组:" & Join(a, ",") & vbCrLf
'True,只把包含5的元素附给B数组
b = Filter(a, 5, True)
Debug.Print "包含5的数组:" & Join(b, ",")
' 原数组:0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60
'
' 只包含5的数组:5,15,25,35,45,50,51,52,53,54,55,56,57,58,59
End Sub