Excel 如何比较两个范围值是否相等?

Excel 如何比较两个范围值是否相等?,excel,vba,Excel,Vba,下面是我的代码 Data = wb.Worksheets(1).Range("B3:E6").Value targetValue = ActiveSheet.Range(targetcellL, targetcellR).Value If Data = targetValue Then MsgBox "Match Found" End If if条件给我的错误为“运行时错误13类型不匹配” 如何比较两个范围值?您必须检查每个项目 如下所示(您可能需要添加一些检查以确保阵列大小相同

下面是我的代码

Data = wb.Worksheets(1).Range("B3:E6").Value
targetValue = ActiveSheet.Range(targetcellL, targetcellR).Value

If Data = targetValue Then
     MsgBox "Match Found"

End If
if条件给我的错误为“运行时错误13类型不匹配”
如何比较两个范围值?

您必须检查每个项目

如下所示(您可能需要添加一些检查以确保阵列大小相同):

下面是heleper
DoArraysMatch()
函数:

Function DoArraysMatch(arr1 As variant, arr2 As Variant) As Boolean
    Dim i As Long, j As Long
    Dim match As Boolean

    match = True
    For i = LBound(arr1,1) to UBound(arr1,1)
        For j = LBound(arr1,2) to UBound(arr1,2)    
            If arr1(i, j) <> arr2(i, j) Then
                match = False
                Exit For
            End If
        Next
        if Not match Then Exit For
    Next
    DoArraysMatch = match
End Function
函数DoArraysMatch(arr1作为变量,arr2作为变量)作为布尔值
我和我一样长,我和我一样长
作为布尔值的Dim匹配
匹配=真
对于i=LBound(arr1,1)到UBound(arr1,1)
对于j=LBound(arr1,2)到UBound(arr1,2)
如果arr1(i,j)arr2(i,j),那么
匹配=错误
退出
如果结束
下一个
如果不匹配,则退出
下一个
DoArraysMatch=匹配
端函数

您必须检查每个项目

如下所示(您可能需要添加一些检查以确保阵列大小相同):

下面是heleper
DoArraysMatch()
函数:

Function DoArraysMatch(arr1 As variant, arr2 As Variant) As Boolean
    Dim i As Long, j As Long
    Dim match As Boolean

    match = True
    For i = LBound(arr1,1) to UBound(arr1,1)
        For j = LBound(arr1,2) to UBound(arr1,2)    
            If arr1(i, j) <> arr2(i, j) Then
                match = False
                Exit For
            End If
        Next
        if Not match Then Exit For
    Next
    DoArraysMatch = match
End Function
函数DoArraysMatch(arr1作为变量,arr2作为变量)作为布尔值
我和我一样长,我和我一样长
作为布尔值的Dim匹配
匹配=真
对于i=LBound(arr1,1)到UBound(arr1,1)
对于j=LBound(arr1,2)到UBound(arr1,2)
如果arr1(i,j)arr2(i,j),那么
匹配=错误
退出
如果结束
下一个
如果不匹配,则退出
下一个
DoArraysMatch=匹配
端函数

因为我知道Data.value和target.value的范围是相同的,所以有任何简单的方法可以做到这一点。您应该限定short对您意味着什么。如果它都是关于短时间,那么提供的代码是相当快的。如果您想要的是一些简短的代码,而不是使用“主”代码,那么请参阅编辑后的答案,了解如何使用
DoArraysMatch()
helper函数。您可以让“看不见”,因为我知道Data.value和target.value具有相同的范围。您应该限定简短的含义。如果它都是关于短时间,那么提供的代码是相当快的。如果您想要的是一些简短的代码,而不是管道工的“主”代码,那么请参阅编辑后的答案,了解如何使用
DoArraysMatch()
helper函数,您可以让“视线之外”
Function DoArraysMatch(arr1 As variant, arr2 As Variant) As Boolean
    Dim i As Long, j As Long
    Dim match As Boolean

    match = True
    For i = LBound(arr1,1) to UBound(arr1,1)
        For j = LBound(arr1,2) to UBound(arr1,2)    
            If arr1(i, j) <> arr2(i, j) Then
                match = False
                Exit For
            End If
        Next
        if Not match Then Exit For
    Next
    DoArraysMatch = match
End Function
Function ArraysEqual() As Boolean
    Dim cell As Range, rng1 As Range, rng2 As Range
    ArraysEqual = True
    For Each cell In rng1
        If WorksheetFunction.CountIf(rng2, cell.Value) = 0 Then
            ArraysEqual = False
            Exit Function
        End If
    Next
End Function