比较两列时为什么会出现运行时错误?

时间:2017-12-11 15:15:43

标签: excel vba excel-vba

我有一个宏应该比较两个不同单元格中的两列。当我测试它时,它首先工作。 (请忽略德语笔记)

两个工作表都有8列,但我只想比较第一列。我已经标记了我得到的行

  

运行时错误424

有人可以帮忙吗?

'Objekte festlegen
Dim j As Integer
Dim d1 As Object
Dim d2 As Object
Dim d3 As Object
Dim e As Range
Dim shA As Worksheet
Dim shB As Worksheet

Set d1 = CreateObject("scripting.dictionary")
Set d2 = CreateObject("scripting.dictionary")
Set d3 = CreateObject("scripting.dictionary")
Set shA = Worksheets(Format(Date, "dd.mm.yyyy"))
Set shB = Worksheets(ActiveSheet.Index - 1)

'Füge ICM Nummern des alten Tabellenblattes Objekten zu
With shB
    For Each e In .Cells(2, 1).Resize(Cells(Rows.Count, 1).End(3).Row).Value
        d1(e) = True
        d2(e) = True
    Next e
End With

'Neue und alte ICM Nummern bestimmen
With shA
  

For each e In .Cells(2,1).Resize(Cells(Rows.Count,   2).END(3).Row)。价值

        If (d2(e)) * (d1.exists(e)) Then d1.Remove e
        If Not d2(e) Then d3(e) = True
    Next e
'Bestimme Anzahl zu erstellender Zeilen
If d1.Count > d3.Count Then
Set j = d1.Count
Else:
Set j = d3.Count
End If
'Füge Zellen ein
Range("1:1").Resize(j).Insert Shift:=xlDown, Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove

'Objekte transponieren und einfügen in aktuelles Tabellenblatte unter ICM Abzug
On Error Resume Next
    .Cells(1, 10).Resize(d1.Count) = Application.Transpose(d1.keys)
    .Cells(1, 11).Resize(d3.Count) = Application.Transpose(d3.keys)
On Error GoTo 0
End With

1 个答案:

答案 0 :(得分:1)

您希望循环使用Range,而不是Value

此外,您需要通过添加Cells作为前缀,使Rows.CountWith shA符合.

变化:

For Each e In .Cells(2, 1).Resize(Cells(Rows.Count, 2).End(3).Row).Value

要:

For Each e In .Cells(2, 1).Resize(.Cells(.Rows.Count, 2).End(3).Row)