Vba 更新Vlookup中的唯一值

Vba 更新Vlookup中的唯一值,vba,excel,duplicates,vlookup,Vba,Excel,Duplicates,Vlookup,我有下面的代码,它使用“UG列表”作为源代码,并在两个不同的表单上执行vlookup-延迟和TT 如果找到结果,则将字符串“UG”传递到每个工作表的特定列上 问题是,即使存在重复的值,字符串“UG”也会被更新。但我想要的是,“UG”应该被更新为唯一值。它不应该被一次又一次地更新为相同的值 Sub vlookup() Dim cl As Range, Dic As Object Set Dic = CreateObject("Scripting.Dictionary"): Dic.Comparem

我有下面的代码,它使用“UG列表”作为源代码,并在两个不同的表单上执行vlookup-延迟和TT

如果找到结果,则将字符串“UG”传递到每个工作表的特定列上

问题是,即使存在重复的值,字符串“UG”也会被更新。但我想要的是,“UG”应该被更新为唯一值。它不应该被一次又一次地更新为相同的值

Sub vlookup()
Dim cl As Range, Dic As Object
Set Dic = CreateObject("Scripting.Dictionary"): Dic.Comparemode = vbTextCompare
With Sheets("Latency")
For Each cl In .Range("B2:B" & .Cells(Rows.count, "C").End(xlUp).Row)
    If Not Dic.exists(cl.Value) Then Dic.Add cl.Value, cl.Row
Next cl
End With
With Sheets("UG list")
For Each cl In .Range("A2:A" & .Cells(Rows.count, "A").End(xlUp).Row)
    If Dic.exists(cl.Value) Then
        Sheets("Latency").Cells(Dic(cl.Value), 17) = "UG"
    End If
Next cl
End With

With Sheets("TT")
For Each cl In .Range("A2:A" & .Cells(Rows.count, "C").End(xlUp).Row)
    If Not Dic.exists(cl.Value) Then Dic.Add cl.Value, cl.Row
Next cl
End With
With Sheets("UG list")
For Each cl In .Range("A2:A" & .Cells(Rows.count, "A").End(xlUp).Row)
    If Dic.exists(cl.Value) Then
        Sheets("TT").Cells(Dic(cl.Value), 23) = "UG"
    End If
Next cl
End With

Set Dic = Nothing
End Sub
两件事:


  • 您对不同的工作表使用相同的字典
    Dic
    。在将其用于下一页之前,请清除字典,这样就不会有任何旧值

    dic.RemoveAll
    附页(“TT”)
    ……代码>


  • 为了防止第二次更新,只要在第一次找到该项时将其从字典中删除即可。虽然我不确定你们指的是什么样的重复值,就像在字典里一样,你们不能有重复值

    If dic.exists(cl.Value) Then
        Sheets("Latency").Cells(dic(cl.Value), 17) = "UG"
        dic.Remove (cl.Value)
    End If
    

  • 如果你只是在谈论一个场景,如果Q列中已经有了“UG”,你想跳过这个单元格,那就先检查一下

      If dic.exists(cl.Value) Then
            If Sheets("Latency").Cells(dic(cl.Value), 17) <> "UG" Then
                Sheets("Latency").Cells(dic(cl.Value), 17) = "UG"
            End If
        End If
    
    如果dic.存在(cl.值),则
    如果表(“延迟”)单元格(dic(cl.Value),17)“UG”,则
    表(“延迟”)。单元格(dic(cl.Value),17)=“UG”
    如果结束
    如果结束