我想在Excel中比较两个范围(范围1和范围2),并在其中一个中列出或发布缺少的数字列表。
例如:范围 1= 1,2,3,4,5,6 范围 2= 1,3,4,6
我想显示 2,5 作为缺失的数字
请帮助我使用Excel VBA实现它们
我不知道
是否有更好的方法,但这会在单元格 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
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