VBA for Excel : Error 424 Object Required

标签 vba excel

我正在完成一个脚本,该脚本验证 Sheet1 的 A 列(“INCIDENTS”)中的单元格是否在 Sheet2 的 A 列(“INCDB”)重复,如果单元格重复,它将删除 Sheet1 中的整行。

问题是在第一个循环(并删除该行)之后,它给了我 424 错误并突出显示 If iSrc.Cells.Value = iDst.Cells.Value Then
关于原因的任何想法?这是代码:

Sub CTDeleteDuplicatePaste()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim iSrc As Variant
Dim iDst As Variant
Dim rng As Range

Set ws1 = Sheets("INCIDENTS")
Set ws2 = Sheets("INCDB")

For Each iSrc In ws1.Range("A5:A9999" & LastRow)
    For Each iDst In ws2.Range("A5:A9999")
        If iSrc.Cells.Value = iDst.Cells.Value Then
        If rng Is Nothing Then
            Set rng = iSrc.EntireRow
        Else
            Set rng = Union(rng, iSrc.EntireRow)
        End If
            rng.EntireRow.Delete

        End If

    Next iDst
Next iSrc

End Sub

最佳答案

我会在没有对象的情况下这样做 iSrciDst .从相反的顺序 - 这段代码对我有用:

Sub CTDeleteDuplicatePaste()

Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim i As Long
Dim j As Long

Set ws1 = Sheets("INCIDENTS")
Set ws2 = Sheets("INCDB")

For i = 9 To 5 Step -1        'change 9 to 9999 for your real data
    For j = 9 To 5 Step -1    'change 9 to 9999 for your real data
        If Len(ws1.Cells(i, 1).Value) > 0 Then
            If ws1.Cells(i, 1).Value = ws2.Cells(j, 1).Value Then
                ws1.Cells(i, 1).EntireRow.Delete
                GoTo nextIteration
            End If
        End If
    Next
nextIteration:
Next

End Sub

关于.EntireRow.Delete的性能问题,这是附加阅读:
Tests on processing 1 million rows
Solution employing Sorting

关于VBA for Excel : Error 424 Object Required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33355273/

相关文章:

excel - DialogSheets ("Name").Labels.Visible = True 不适用于 Excel VBA 中的所有 DialogSheets

excel - 文件夹中有 PDF。需要在Excel中记录名称和位置

excel - 复制数据透视表格式

excel - 有没有办法使用 Excel 以编程方式使用动态数据定义表区域?

Perl - 无法访问 Excel 文件

excel - 在 Excel 中剪切/粘贴同一行中的重复金额

excel - 确定字符串是否是某个范围内的日期前缀的算法

excel - 检查 Excel 对象是否没有主题

vba - 尝试使用 Selenium VBA 自动化 Chrome,自动化错误?

vba - 在单元格中写入 VBA 宏速度缓慢