Countifs对象未find错误424

我在这里是新的,新的vba。 我试着做一个countif,如果答案是超过0的标准是尊重,所以我有超过500行双重检查问题。

Sub DoubleCheck() 'Variables declaration Dim row_rounter As Long, rngQ As Range, cel As Range, resDblChq As Variant, rngB As Range, rngC As Range, rngF As Range, rngH As Range, rngI As Range row_counter = Sheets("GW-DB").Cells(Cells.Rows.Count, "B").End(xlUp).Row 'row counter 'Range Initializatoin Set rngQ = Sheets("GW-DB").Range("Q2:Q" & row_counter) Set rngC = Sheets("GW-DB").Range("C2:C" & row_counter) Set rngF = Sheets("GW-DB").Range("F2:F" & row_counter) Set rngH = Sheets("GW-DB").Range("H2:H" & row_counter) Set rngI = Sheets("GW-DB").Range("I2:I" & row_counter) Set rngB = Sheets("GW-DB").Range("B2:B" & row_counter) 'Loop starts to validate possibility of double cheques For Each cel In rngQ ' countif same name, same date,same amount, same reason resDblChq = Application.WorksheetFunction.CountIfs(rngB, c.Offset(0, -16).Value, rngC, c.Offset(0, -15).Value, rngF, c.Offset(0, -12).Value, rngH, c.Offset(0, -9).Value, rngI, c.Offset(0, -8).Value) ' if it counts more than 0 it means there's a possibility of having double cheques issued If resDblChq > 0 Then c.Value = "Possible payment made twice" End If Next cel End Sub 

谢谢你,我想保持最简单的方式进行进一步的修改

在你的循环中,你正在引用C而不是Cel 。 尝试改变这个。 这将产生错误424。

 Sub DoubleCheck() 'Variables declaration Dim row_rounter As Long, rngQ As Range, cel As Range, resDblChq As Variant, rngB As Range, rngC As Range, rngF As Range, rngH As Range, rngI As Range row_counter = Sheets("GW-DB").Cells(Cells.Rows.Count, "B").End(xlUp).Row 'row counter 'Range Initializatoin Set rngQ = Sheets("GW-DB").Range("Q2:Q" & row_counter) Set rngC = Sheets("GW-DB").Range("C2:C" & row_counter) Set rngF = Sheets("GW-DB").Range("F2:F" & row_counter) Set rngH = Sheets("GW-DB").Range("H2:H" & row_counter) Set rngI = Sheets("GW-DB").Range("I2:I" & row_counter) Set rngB = Sheets("GW-DB").Range("B2:B" & row_counter) 'Loop starts to validate possibility of double cheques For Each cel In rngQ ' countif same name, same date,same amount, same reason resDblChq = Application.WorksheetFunction.CountIfs(rngB, cel.Offset(0, -16).Value, rngC, cel.Offset(0, -15).Value, rngF, cel.Offset(0, -12).Value, rngH, cel.Offset(0, -9).Value, rngI, cel.Offset(0, -8).Value) ' if it counts more than 0 it means there's a possibility of having double cheques issued If resDblChq > 0 Then cel.Value = "Possible payment made twice" End If Next cel End Sub 

用cel而不是c

  resDblChq = Application.WorksheetFunction.CountIfs(rngB, cel.Offset(0, -16).Value, rngC, cel.Offset(0, -15).Value, rngF, cel.Offset(0, -12).Value, rngH, cel.Offset(0, -9).Value, rngI, cel.Offset(0, -8).Value) 

c是未声明的,因为没有指定Option Explicit ,所以在运行时它是一个隐含的Variant ,它包含了vbEmpty

  Debug.Assert Not IsEmpty(c) ' assertion fails here Debug.Print c.Offset(0, -16).Value '"object required" 

当然,你可以使用Cel而不是c ,但是这并不能防止其他烦人的, 容易避免的运行时错误,只是在每个模块的顶部指定Option Explicit

VBA会高兴地编译错别字,并将它们声明为dynamic隐式Variantvariables,而不Option Explicit指定的Option Explicit :这不是您如何编写可靠的代码。 使用。 选项。 明确。

错误提示“object required”的原因是因为在语法上, c.Offset(0, -16).Value只能是一个名为c的对象的Property GetFunction成员调用 – 但IsObject(c) False ,因为vbEmpty不是一个对象引用。 因此, 需要的对象