我想在Excel中比较两个范围(范围1和范围2)并列出或发布其中任何一个中缺失数字的列表。
例如:范围1 = 1,2,3,4,5,6 范围2 = 1,3,4,6
我想将2,5显示为缺失的数字
请帮我用Excel VBA实现它们
答案 0 :(得分:0)
我不知道是否有更好的方法,但这会在Cell 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
答案 1 :(得分:0)
Mutjaylee的回答不起作用。它仅检查范围1中的所有元素是否在范围2中。如果String_Array(I)= 1,则InStr(1,Range(“B1”),String_Array(I))将返回true,如果Range(“B1”)= “-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