简体   繁体   English

Excel VBA Variant数组查找

[英]Excel vba Variant array lookup

I have two (non empty) arrays (of variants) with numbers. 我有两个(带变体的)数字数组(非空)。 I would like to list all the data that is in the first array and is not in the second array. 我想列出所有在第一个数组中而不是在第二个数组中的数据。

Dim existingWorkerIDs() As Variant
Dim newWorkerIDs() As Variant

  For Each temp In newWorkerIDs

        If existingWorkerIDs.contains(temp) Then
            ...do sth...
        End If

   Next temp

Is it possible? 可能吗?

Easily doable by abusing MATCH . 可以通过滥用MATCH轻松实现。

The first procedure is just a test for verification, and also an example of how things have to be declared (and conversely, what declarations you would have to change if you need other variable types, etc). 第一个过程只是一个验证测试,也是一个如何声明事物的示例(相反,如果需要其他变量类型,则必须更改哪些声明等)。

Sub testCaller()
    Dim testArr1() As Variant ' <~~ Variable type must match
    Dim testArr2() As Variant '     the variable required in the 
    Dim testArr3() As Variant '     actual procedure
    Dim testArr4() As Variant


    testArr1 = Array("abc", "abc", "def", "abc", "asdf", "bcd")
    testArr2 = Array("abc", "asdf")
    Call listUniqueArrayContents(testArr1(), testArr2())

    testArr3 = Array(1, 2, 3, 4, 5)
    testArr4 = Array(1, 2)
    Call listUniqueArrayContents(testArr3(), testArr4())
End Sub

Sub listUniqueArrayContents(arr() As Variant, arrCompare() As Variant)
    Dim uniqueValues() As Variant
    Dim mIndex As Variant
    Dim j As Integer

    j = 0

    For i = 0 To UBound(arr())
        ' Reset the placeholder for our MATCH values
        mIndex = Null

        ' Disable errors, otherwise you get popups every time there's a unique value
        On Error Resume Next

        ' Call MATCH function
        mIndex = Application.WorksheetFunction.match(arr(i), arrCompare(), 0)

        ' Restore normal error handling
        On Error GoTo 0

        If mIndex < 1 Or IsNull(mIndex) Then
            ' If match variable is Null, it means the value was unique
            ' So we'll write that value to a separate array to keep track of it
            If j = 0 Then ReDim Preserve uniqueValues(0 To 0)
            If j <> 0 Then ReDim Preserve uniqueValues(UBound(uniqueValues()) + 1)
            uniqueValues(UBound(uniqueValues)) = arr(i)
            j = j + 1
        End If
    Next i

    Debug.Print "--Unique values:--"
    For k = LBound(uniqueValues()) To UBound(uniqueValues())
        Debug.Print uniqueValues(k)
    Next k
    Debug.Print "--End--"
End Sub

Which, for the test examples, gives you the expected: 对于测试示例,可以给您以下期望:

--Unique values:-- -唯一值:-
def 定义
bcd 光盘
--End-- - 结束 -
--Unique values:-- -唯一值:-
3 3
4 4
5 5
--End-- - 结束 -

Alternatively, you can change this into a Function and have it return the array of unique values. 或者,您可以将其更改为Function并使其返回唯一值数组。

Change this: 更改此:
Sub listUniqueArrayContents(arr() As Variant, arrCompare() As Variant)

to this: 对此:
Function listUniqueArrayContents(arr() As Variant, arrCompare() As Variant) As Variant

and replace the last-most For -loop with listUniqueArrayContents = uniqueValues() 并将最新的For -loop替换为listUniqueArrayContents = uniqueValues()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM