我正在尝试比较不同工作表中的两列,以检查列A中是否存在列A中的值。
当我试图转置行值并将其存储到字典中进行比较时,会触发运行时错误“13”。
以下是代码:
Sub Compare()
Dim rngA As Range, rngB As Range
Dim dict As Object, rw As Range
Dim a As Application, tmp As String
Dim Row1Last As Long
Dim Row2Last As Long
Set a = Application
Set dict = CreateObject("scripting.dictionary")
Set sht1 = Worksheets("list_Facility_BOS")
Set sht2 = Worksheets("List_Facility_PG")
With Sheets("list_Facility_BOS")
Row1Last = .Cells(Rows.Count, "G").End(xlUp).Row
End With
With Sheets("List_Facility_PG")
Row2Last = .Cells(Rows.Count, "H").End(xlUp).Row
End With
Set rngA = sht1.Range("G2:G" & Row1Last)
Set rngB = sht2.Range("H2:H" & Row2Last)
For Each rw In rngA.Rows
dict.Add Join(a.Transpose(a.Transpose(rw.Value)), Chr(0)), rw.Row
Next rw
For Each rw In rngB.Rows
tmp = Join(a.Transpose(a.Transpose(rw.Value)), Chr(0))
If dict.exists(tmp) Then
Else
rw.Cells(1).Interior.ColorIndex = 3
End If
Next rw
End Sub
我尝试将20k行记录列与另一个工作表中的另一个20k行列进行比较。错误'13'是这行代码中的触发器:
dict.Add Join(a.Transpose(a.Transpose(rw.Value)), Chr(0)), rw.Row
我是擅长VBA编程的人,对不起,如果我不能清楚地解释它。如果我的代码中有任何错误,请告诉我。
答案 0 :(得分:0)
这个功能的解释在丢失的同时被复制了几次。 这是我认为的原始fastest-way-to-find-a-row-in-excel-range-using-excel-macro-vba。
查看评论
For Each rw In rngA.Rows
'Create a key from the entire row and map to row
' If your rows are not unique you'll have to do a bit more work here
' and use an array for the key value(s)
dict.Add Join(a.Transpose(a.Transpose(rw.Value)), Chr(0)), rw.Row
Next rw
您需要rngA
中的唯一行,否则会得到一个导致错误13的数组。
见Using-the-Dictionary-Class-in-VBA
使用数据库而不是Excel文件可以避免这种问题。