首页 > 解决方案 > 检查重复项时出现 VBA 运行时错误 1004“应用程序定义或对象定义错误”

问题描述

我遇到了错误 1004“应用程序定义或对象定义错误”的问题。

我以前多次使用完全相同的代码,但由于某种原因,我无法让它在这个 sub 中起作用。

Sub Checkduplicates()

    Dim tracker As Workbook

    Set tracker = ActiveWorkbook

    With tracker.Sheets("Tracker")

        Dim sEntity As String, sAmt As Double, sRow As Integer

        sEntity = .Cells(Row, 6).Value
        sAmt = .Cells(Row, 11).Value

        If Row > 1010 Then sRow = Row - 1000 Else sRow = 4

        For sRow = sRow To Row - 1

            If .Cells(sRow, 6).Value = sEntity And .Cells(sRow, 11).Value = sAmt Then

                  Call GetAnswer

            End If
        Next sRow

    End With

End Sub

标签: excelvba

解决方案


rowin sEntity = .Cells(row, 6).ValueandsAmt = .Cells(row, 11).Value是未声明和未定义的。看起来它应该是 F 列中最后一个填充的单元格。

Sub Checkduplicates()

    Dim tracker As Workbook

    Set tracker = ActiveWorkbook

    With tracker.Sheets("Tracker")

        Dim sEntity As String, sAmt As Double, sRow As LONG, rw as LONG

        rw = .cells(.rows.count, "F").end(xlup).row
        sEntity = .Cells(rw, 6).Value
        sAmt = .Cells(rw, 11).Value

        If Row > 1010 Then sRow = Row - 1000 Else sRow = 4

        For sRow = sRow To rw - 1

            If .Cells(sRow, 6).Value = sEntity And .Cells(sRow, 11).Value = sAmt Then

                  Call GetAnswer

            End If
        Next sRow

    End With

End Sub

使用选项显式。


推荐阅读