2016-06-09 124 views

回答

0

我不知道是否有更好的方法實現這些目標,但是這將在單元格C1

Sub Missing() 
    String_Array = Split(Range("A1"), ",") 
    For I = 0 To UBound(String_Array) 
     If InStr(1, Range("B1"), String_Array(I)) = 0 Then 
      If Range("C1") <> Empty Then Range("C1") = Range("C1") & "," 
      Range("C1") = Range("C1") & String_Array(I) 
     End If 
    Next I 
End Sub 
0

Mutjaylee的答案不起作用寫失蹤的數字。它只檢查範圍1中的所有元素是否在範圍2中。如果String_Array(I)= 1,則如果範圍(「B1」)= 1,則InStr(1,Range(「B1」),String_Array 「2,3,10」

試試這個:

Sub Missing() 
    Dim resultString As String 
    Dim arr1() As String 
    Dim arr2() As String 
    arr1 = Split(Range("A1"), ",") 
    arr2 = Split(Range("B1"), ",") 

    CompareArrays arr1, arr2, resultString 
    CompareArrays arr2, arr1, resultString 

    Range("C1") = resultString 
End Sub 


Sub CompareArrays(arr1() As String, arr2() As String, resultString As String) 
    Dim i As Integer, j As Integer 
    Dim bFlag As Boolean 
    For i = 0 To UBound(arr1) 
     bFlag = False 
     For j = 0 To UBound(arr2) 
      If arr1(i) = arr2(j) Then 
       bFlag = True 
       Exit For 
      End If 
     Next 
     If Not bFlag Then 
      If (Len(resultString)) Then resultString = resultString & "," 
      resultString = resultString & arr1(i) 
     End If 
    Next 

End Sub 
相關問題