我想在此比较中添加另一列

时间:2019-08-08 13:09:11

标签: excel vba

我正在尝试通过构建两个字典来比较两张纸,我对它和它的速度感到非常满意,但是我想在比较中再增加两列,没有人知道如何添加它。 / p>

这个想法是,如果A列,sheet1与B列,sheet2匹配,但C列sheet1与D,sheet2列不匹配,则在sheet2的F列上打印“ N”,如果它们都与在F2的F列上打印“ Y”相匹配工作表2。

Sub Compare()
    Dim Rng As Range
    Dim RngList As Object
    Dim Sht1 As Worksheet
    Dim Sht2 As Worksheet

    Set RngList = CreateObject("scripting.dictionary")
    Set Sht1 = Worksheets("Sheet1")
    Set Sht2 = Worksheets("Sheet2")

    With RngList
        .CompareMode = vbTextCompare
        For Each Rng In Sht1.Range("F2", Sht1.Range("F" &  Rows.Count).End(xlUp))
            If Not .exists(Rng.Value) Then .Add Rng.Value, Rng.Row
        Next Rng

        Worksheets("Current_Month").Activate

        For Each Rng In Sht2.Range("F2", Sht2.Range("F" & Rows.Count).End(xlUp))  
            If .exists(Rng.Value) Then
                If Rng.Offset(, 1) <> Sht1.Range("B" & RngList(Rng.Value)) Then Rng.Offset(, 5).Cells = "N"                
            Else
                Rng.Offset(, 5).Cells = "Y"
            End If
        Next Rng
    End With

    Set RngList = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

因此,如果有人需要,我可以通过执行此操作来弄清楚,但还是谢谢您。

  For Each Rng In Sht1.Range("F2", Sht1.Range("F" & Rows.Count).End(xlUp))   
        If Not .exists(Rng.Value) Then .Add Rng.Value, Rng.Row
    Next

     For Each Rng In Sht1.Range("E2", Sht1.Range("E" & Rows.Count).End(xlUp)) 
        If Not .exists(Rng.Value) Then .Add Rng.Value, Rng.Row

    Next