简体   繁体   English

比较Excel Vba中的2个动态命名范围

[英]Comparing 2 dynamic named ranges in Excel Vba

I want to compare 2 dynamic named ranges (OriData and Filterdata), and set Filterdata into ""(empty) if OriData and Filterdata are exactly identical (Filterdata is the results of twice advance filter of OriData, I want to zero it if both of them are identical). 我想比较2个动态命名范围(OriData和Filterdata),如果OriData和Filterdata完全相同(Filterdata是OriData的两倍超前过滤的结果,我想将其归零),则将Filterdata设置为“”(空)它们是相同的)。 I barely manage to come out with this code but it always shows "Disimilar ranges". 我勉强能拿出这段代码,但是它总是显示“ Disimilar range”。 What's wrong with my code and I would appreciate deeply if you can come out with the edited code to suit my need (zero the Filterdata if identical) 我的代码有什么问题,如果您能根据我的需要提出修改后的代码,我将不胜感激(如果相同,则将Filterdata设置为零)

Sub Santa()
    Dim Dn As Range

    For Each Dn In Range("OriData")

        If Not Dn = Range("Filterdata").Cells(Dn.Row, Dn.Column) Then MsgBox "Disimilar ranges": Exit Sub

    Next Dn

    MsgBox "Both Ranges have the same data"

End Sub

Im really new in vba so please bear with me... Thanks in advance! 我在vba中真的很新,所以请多包涵...预先感谢!

The Dn.Row and Dn.Column are the row and column on the worksheet, not the relative row within Range("OriData"). Dn.RowDn.Column是工作表上的行和列,而不是Range(“ OriData”)中的相对行。 When you try to reference the sister cell in Range("Filterdata"), you are not referencing the same cell position. 当您尝试引用Range(“ Filterdata”)中的姊妹单元格时,您未引用相同的单元格位置。

Sub Santa()
    Dim r As Long, c As Long

    If Range("OriData").Count <> Range("Filterdata").Count Then
        MsgBox "Dissimilar range sizes"
        Exit Sub
    End If

    For r = 1 To Range("OriData").Rows.Count
        For c = 1 To Range("OriData").Columns.Count
            If Not Range("OriData").Cells(r, c) = Range("Filterdata").Cells(r, c) Then
                MsgBox "Dissimilar range values"
                Exit Sub
            End If
        Next c
    Next r

    MsgBox "Both Ranges have the same data"

End Sub

This uses a diffent approach. 这使用了不同的方法。 When the ranges are huge, looping through the worksheet is inefficient. 当范围很大时,在工作表中循环效率很低。 Here, the ranges are copied into variables first, then compared. 在此,范围首先被复制到变量中,然后进行比较。 If identical, the named range is either cleared (the values are deleted) or deleted (values and named range) - uncomment if needed. 如果相同,则清除命名范围(删除值)或删除(值和命名范围)-如果需要,请取消注释。

Error checking is included. 包括错误检查。

The code uses meaningful variable names to help with understanding and maintaining it over time: 该代码使用有意义的变量名来帮助您逐步理解和维护它:

Sub DeleteIfIdentical()
    Dim r As Long, c As Long
    Dim ub1 As Long, ub2 As Long
    Dim original, filtered   ' arrays holding the named ranges' values

    On Error GoTo NoSuchRange
    original = Range("original")
    filtered = Range("filtered")
    On Error GoTo 0

    ub1 = UBound(original, 1)
    ub2 = UBound(original, 2)
    If Not (UBound(filtered, 1) = ub1) And _
           (UBound(filtered, 2) = ub2) Then
        MsgBox "Ranges are not identical!"
        Exit Sub
    End If

    For r = 1 To ub1
        For c = 1 To ub2
            If original(r, c) <> filtered(r, c) Then
                MsgBox "Ranges are not identical!"
                Exit Sub
            End If
        Next c
    Next r

    MsgBox "Both Ranges have the same data"
    Range("filtered").Clear   ' to clear all values
    '   Range("filtered").Delete  ' to clear all values and delete the named range
    Exit Sub

NoSuchRange:
    MsgBox "Error accessing named ranges 'orginal' and/or 'filtered'"
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM