2016-04-06 14 views
0

Ich habe zwei (nicht leere) Arrays (von Varianten) mit Zahlen. Ich möchte alle Daten auflisten, die sich im ersten Array und nicht im zweiten Array befinden.Excel VBA Variant Array Lookup

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

    For Each temp In newWorkerIDs 

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

    Next temp 

Ist es möglich?

+0

könnte es einfacher sein, ein Wörterbuch zu verwenden. Schau mal hier: http://stackoverflow.com/questions/915317/does-vba-have-dictionary-structure – sous2817

Antwort

0

Leicht machbar durch Missbrauch MATCH.

Die erste Prozedur ist nur ein Test zur Verifizierung, und auch ein Beispiel dafür, wie Dinge deklariert werden müssen (und umgekehrt, welche Deklarationen Sie ändern müssten, wenn Sie andere Variablentypen benötigen, usw.).

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 

, durch die für die Testbeispiele, gibt Ihnen die erwarteten:

--Einzigartig Werte: -
def
bcd
--End--
--Einzigartig Werte: -
--End--

Alternativ können Sie dies in eine Function ändern und das Array mit eindeutigen Werten zurückgeben lassen. diese

Wechsel:
Sub listUniqueArrayContents(arr() As Variant, arrCompare() As Variant)

dazu:
Function listUniqueArrayContents(arr() As Variant, arrCompare() As Variant) As Variant

und ersetzen die zuletzt die meisten For -loop mit listUniqueArrayContents = uniqueValues()

Verwandte Themen