VBA Excel中,两个数组查找匹配该怎么写

就是数组A("A","B","C","D"),数组B("A","B"),需要查找到数组A哪些值不包括在数组B中,则数组C=("C","D"), 真心求教!

那只有用循环来做!
双重循环。
Option Explicit

Private Sub Command1_Click()
Dim A, B
Dim C() As String
Dim i As Integer
Dim j As Integer
Dim n As Integer

A = Array("A", "B", "C", "D")
B = Array("A", "B")

'采用循环嵌套进行查核
n = -1
For i = 1 To UBound(A)
For j = 1 To UBound(B)
If A(i) = B(j) Then Exit For
Next j
If j > UBound(B) Then
n = n + 1
ReDim Preserve C(n) As String
C(n) = A(i)
End If
Next i
'输出数组C
If n > -1 Then
For i = 0 To UBound(C)
Print C(i)
Next i
Else
Print "数组C没有元素!"
End If
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-07-18

就你的这个问题,用循环判断即可,代码如下

Sub test()
    Dim arr, brr, crr()
    Dim x&, y&, i&
    arr = Array("A", "B", "C", "D")
    brr = Array("A", "B")
    For x = 0 To UBound(arr)
        For y = 0 To UBound(brr)
            If arr(x) = brr(y) Then GoTo aa
        Next y
        i = i + 1
        ReDim Preserve crr(1 To i)
        crr(i) = arr(x)
aa:
    Next x
End Sub

 运用到EXCEL中,简单的改动一下即可,请看附件


相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网