excel 如何在同一时间循环所有数组元素?

hxzsmxv2  于 2023-05-08  发布在  其他
关注(0)|答案(3)|浏览(230)

我不能同时循环数组filter_Criteria的所有元素,
所以我必须使用三个If条件来检查我的规则。
我需要剥离这三行代码:

If InStr(arr(r, 1), filter_Criteria(0)) = 0 And _
    InStr(arr(r, 1), filter_Criteria(1)) = 0 And _
    InStr(arr(r, 1), filter_Criteria(2)) = 0 Then

这是代码:

Sub Loop_Array_at_the_same_time()
    
    Dim filter_Criteria() As Variant, dict As Object, arr, r As Long
    
    filter_Criteria = Array("A", "B", "C")
    
    arr = Application.Transpose(Range("B2", Cells(Rows.Count, "B").End(xlUp)))
   
    Set dict = CreateObject("Scripting.Dictionary")

    For r = 1 To UBound(arr)
    
         If InStr(arr(r, 1), filter_Criteria(0)) = 0 And _
            InStr(arr(r, 1), filter_Criteria(1)) = 0 And _
            InStr(arr(r, 1), filter_Criteria(2)) = 0 Then
               
            dict(arr(r, 1)) = vbNullString
                 
         End If
        
    Next r
    
End Sub
0aydgbwb

0aydgbwb1#

这个工作答案的学分@FaneDuru
我应该创建一个布尔变量,在(相同的)迭代中使其为true,而不需要using dict(arr(r, 1)) = vbNullString
在检查之前,我必须有boolFound = false
然后在迭代后检查相应的变量是否为假然后dict(arr(r, 1)) = vbNullString

Sub Loop_Array_at_the_same_time()
    
    Dim filter_Criteria() As Variant, dict As Object, arr, r As Long
    
    filter_Criteria = Array("*A*", "*B*", "*C*")
    
    arr = Application.Transpose(Range("B2", Cells(Rows.Count, "B").End(xlUp)))
   
    Set dict = CreateObject("Scripting.Dictionary")

    Dim boolFound As Boolean, el As Variant

    For r = 1 To UBound(arr)
          boolFound = False
            For Each el In filter_Criteria
                If arr(r, 1) Like el Then boolFound = True: Exit For
           Next el
           If Not boolFound Then dict(CStr(arr(r, 1))) = vbNullString
    Next r
    
End Sub
wfsdck30

wfsdck302#

您可以将过滤器值连接为一个字符串,然后使用like运算符:

Sub look_Array()

Dim filter_Criteria As String
filter_Criteria = "|" & Join(Array("A", "B", "C"), "|") & "|"

Dim arrValues As Variant
arrValues = Application.Transpose(Range("B2", Cells(Rows.Count, "B").End(xlUp)))
   
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

Dim r As Long

For r = 1 To UBound(arrValues, 1)
    If Not filter_Criteria Like "*|" & arrValues(r) & "|*" Then
        dict(arrValues(r)) = vbNullString
    End If
Next

End Sub

为了安全起见,我们需要在字符串的两边都有管道符号。

mf98qq94

mf98qq943#

根据艾克的回答:使用like的建议是好的。然而,试图将实际值转换为模式有点奇怪。您应该创建一个保持不变的值(在本例中是A、B和C)的模式,并将该模式与值进行比较。
我们创建的模式如下。[A,B,C]这将匹配这些字符中的任何一个,所以如果一个字符串包含A,这部分将匹配,如果一个字符串包含B,这部分将匹配,等等。为了说明所有可能出现在我们要查找的字符之前或之后的字符,我们只需在模式的前面和后面放置一个*(* 表示任何字符的0或更多(因此基本上匹配任何字符,这就是我们想要的))

Sub look_Array()

Dim filter_Criteria As String
filter_Criteria = "|*[" & Join(Array("A", "B", "C"), ",") & "]*|"

Dim arrValues As Variant
arrValues = Application.Transpose(Range("B2", Cells(Rows.Count, "B").End(xlUp)))
   
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

Dim r As Long

For r = 1 To UBound(arrValues, 1)
    If Not "|" & arrValues(r) & "|" Like filter_Criteria Then
        dict(arrValues(r)) = vbNullString
    End If
Next
End Sub

相关问题