Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Excel中比较两个范围_Excel_Vba - Fatal编程技术网

在Excel中比较两个范围

在Excel中比较两个范围,excel,vba,Excel,Vba,我试图比较两个范围的数据。“A”包含1,2,3,4,“B”包含1,2,5。我想找到那些在“B”中但不在“A”中的,即5。下面是我的代码,但我似乎无法得到我想要的。有人能给点建议吗 Dim a As Range, b As Range, i As Integer, x As Integer Set a = Range("A1:A4") Set b = Range("B1:B3") For i = 1 To b.Count For x = 1 To a.Count If b(i, 1

我试图比较两个范围的数据。“A”包含1,2,3,4,“B”包含1,2,5。我想找到那些在“B”中但不在“A”中的,即5。下面是我的代码,但我似乎无法得到我想要的。有人能给点建议吗

Dim a As Range, b As Range, i As Integer, x As Integer
Set a = Range("A1:A4")
Set b = Range("B1:B3")

For i = 1 To b.Count
  For x = 1 To a.Count

    If b(i, 1) = a(x, 1) Then

    Else
      MsgBox (b(i, 1))
      Exit For
    End If
  Next x
Next i

这是一个非常小的范围,但我仍然建议使用数组存储范围值,然后使用数组进行比较。看看这个例子

Sub Sample()
    Dim Ar1, Ar2
    Dim i As Long, j As Long
    Dim Found As Boolean

    Ar1 = Range("A1:A4"): Ar2 = Range("B1:B3")

    For i = LBound(Ar2) To UBound(Ar2)
        Found = False
        For j = LBound(Ar1) To UBound(Ar1)
            If Ar1(j, 1) = Ar2(i, 1) Then
                Found = True
                Exit For
            End If
        Next j

        If Found = False Then Debug.Print Ar2(i, 1) & " Is unique"
    Next i
End Sub

编辑

另一种方式(不过我还是喜欢上面的方式)


或者,如果您想要使用Excel工作表函数的非VBA解决方案,请在类似C列的列中尝试此公式

=IF(ISERROR(FIND(B:B,A:A)),B:B&“未找到”和“”)


Philip

我倾向于选择像Find()这样的原生VBA函数。我发现它们的使用速度要快一点,尤其是在大范围工作时

下面是另一个解决方案:

Option Explicit
Sub test()
    Dim r1 As Excel.Range
    Dim r2 As Excel.Range

    Set r1 = Sheet1.Range("A1:A4")
    Set r2 = Sheet1.Range("B1:B3")

    Dim s() As String

    s = getUniques(r1, r2)

    Dim i As Long
    For i = 0 To UBound(s)
        Debug.Print s(i)
    Next i
End Sub


Function getUniques(ByRef r1 As Excel.Range, ByRef r2 As Excel.Range) As String()
    Dim cell As Excel.Range
    Dim found As Excel.Range

    Dim uniques() As String
    Dim i As Long

    For Each cell In r2
        On Error Resume Next
        Set found = r1.Find(cell.Value)
        On Error GoTo 0

        If (found Is Nothing) Then
            ReDim Preserve uniques(i)
            uniques(i) = cell.Value
            i = i + 1
        End If
    Next cell

    getUniques = uniques
End Function

好的1!如果OP已准备好使用非VBA解决方案,我更喜欢您的解决方案,而不是我的解决方案:)
Option Explicit
Sub test()
    Dim r1 As Excel.Range
    Dim r2 As Excel.Range

    Set r1 = Sheet1.Range("A1:A4")
    Set r2 = Sheet1.Range("B1:B3")

    Dim s() As String

    s = getUniques(r1, r2)

    Dim i As Long
    For i = 0 To UBound(s)
        Debug.Print s(i)
    Next i
End Sub


Function getUniques(ByRef r1 As Excel.Range, ByRef r2 As Excel.Range) As String()
    Dim cell As Excel.Range
    Dim found As Excel.Range

    Dim uniques() As String
    Dim i As Long

    For Each cell In r2
        On Error Resume Next
        Set found = r1.Find(cell.Value)
        On Error GoTo 0

        If (found Is Nothing) Then
            ReDim Preserve uniques(i)
            uniques(i) = cell.Value
            i = i + 1
        End If
    Next cell

    getUniques = uniques
End Function