VBA – 尝试转置行值时运行时错误“13”

我试图比较不同工作表中的两列来检查A列中的值是否存在于B列中。

当我尝试转置行值并将其存储到字典进行比较时,会触发运行时错误“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行logging列与另一个工作表中另一个20k行列。 错误“13”在这行代码中被触发:

 dict.Add Join(a.Transpose(a.Transpose(rw.Value)), Chr(0)), rw.Row 

我是新来的优秀的VBA编程,抱歉,如果我不能解释清楚。 请让我知道,如果我的代码有任何错误。

function的解释丢失了,而被复制了几次。 这是我认为最快的方法,find一个在Excel中的行范围使用Excelmacrosmacrovba 。

看看评论

 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.请参阅使用 rngA -Class-in-VBA

这种问题可以避免,使用数据库而不是Excel文件。