我正在尝试使用Dictionary
完成以下任务:(不要问为什么:P,我刚刚找到这个方法并开始使用它并且没有寻找其他可能性,任何更好的方法欢迎)
检查表格“DRG”中的Col AE是否为字符串“TRUE”。
如果找到,则将工作表“DRG”的Col E中的值与工作表“Latency”的Col R进行比较。
如果在Sheet“Latency”中找到匹配项,并且在DRG表中它具有值“TRUE”,则在“Latency”表中更新Col S中的文本“IP”。
< / LI>我有下面的代码,不知道在哪里包含比较表“Latency”的Col R和表“DRG”的Col E的行
我的代码
Sub IPFinder()
Dim cl As Range, Dic As Object
Set Dic = CreateObject("Scripting.Dictionary"): Dic.Comparemode = vbTextCompare
With Sheets("Latency")
For Each cl In .Range("R2:R" & .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("DRG")
For Each cl In .Range("AE2:AE" & .Cells(Rows.Count, "A").End(xlUp).Row)
If Dic.exists(cl.Value) = "TRUE" Then
Sheets("Latency").Cells(Dic(cl.Value), 19) = "IP"
End If
If Dic.exists(cl.Value) Then
Sheets("Latency").Cells(Dic(cl.Value), 19) = "IP"
Dic.Remove (cl.Value)
End If
Next cl
End With
Set Dic = Nothing
End Sub
答案 0 :(得分:1)
按照您的描述:
检查表格中的Col AE&#34; DRG&#34;对于字符串&#34; TRUE&#34;。
如果找到,则比较表格#E; DRG&#34;使用表格的Col R&#34; Latency&#34;。
- 如果在Sheet&#34; Latency&#34;中找到匹配项如果它有 价值&#34; TRUE&#34;在表DRG中,然后更新文本&#34; IP&#34;在Col S in &#34;延迟&#34;片材。
代码应按如下方式调整(见评论):
Option Explicit
Sub IPFinder()
Dim cl As Range, Dic As Object
Set Dic = CreateObject("Scripting.Dictionary"): Dic.Comparemode = vbTextCompare
With Sheets("Latency")
For Each cl In .Range("R2:R" & .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("DRG")
With .Range("AE1:AE" & .Cells(Rows.Count, "A").End(xlUp).Row) '<--| reference its column AE range from row 1 (header) down to the one corresponding to last column A not empty row
.AutoFilter Field:=1, Criteria1:="TRUE" '<--| filter column AE cells with "TRUE" content
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if any cell found
For Each cl In .Resize(.Rows.Count - 1).Offset(1, -26).SpecialCells(xlCellTypeVisible) '<--| loop through column "E" cells correesponding to filtered ones in column "AE" (skipping headers)
If Dic.exists(cl.Value) Then Sheets("Latency").Cells(Dic(cl.Value), "S") = "IP" '<--| if cirrent column E cell value matches any "Latency" sheet column R one then write "IP" in "Latency" sheet corresponding column S cell
Next
End If
End With
.AutoFilterMode = False
End With
Set Dic = Nothing
End Sub