【问题标题】:Countifs object not found error 424Countifs 对象未找到错误 424
【发布时间】:2017-10-19 14:13:39
【问题描述】:

我是新来的,也是 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

谢谢你,我想保留最简单的方法以供进一步修改

【问题讨论】:

  • 我忘了说错误是需要错误 424 对象
  • 它在哪一行抛出错误?
  • 请参阅this answer 以了解为什么您会收到该特定错误,并注意在模块顶部指定Option Explicit 可以防止该错误。
  • 谢谢你所有的 aswner 这真的是因为参考 c。
  • 还有一个问题:我使用 countif 但它返回 0。我查了一下,他们说 countifs,arg1 是范围,arg2 to ... 是标准。但是,如何放置多个范围?

标签: vba excel worksheet-function countif


【解决方案1】:

用 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)

【讨论】:

  • 第一个问题现在已经解决了但是你知道为什么函数返回0吗?范围是准确的。我可以只放一个范围吗?谢谢
  • 是的,您可以尝试一个参数并检查。在检查之前,我建议修剪数据中的字符串,因为您使用的是字符串函数,并且空格很容易被忽略或忽略。
【解决方案2】:

在您的循环中,您引用的是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

【讨论】:

  • 再一次使用option explicit 可以轻松避免的错误之一...
  • 哎呀...我现在看到了 Jarom 的答案...我想我的工具太长了,无法检查答案...
【解决方案3】:

c 未声明,并且由于未指定 Option Explicit,因此在运行时它是一个隐含的 Variant,其中包含 vbEmpty

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

当然可以use Cel instead of c,但这不会阻止无数其他烦人的易于避免运行时错误,这些错误只是在每个模块的顶部指定Option Explicit会的。

VBA 将愉快地编译拼写错误并将它们声明为即时隐式 Variant 变量而不指定 Option Explicit:这不是您编写可靠代码的方式。采用。选项。明确的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-30
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 2017-08-26
    • 2013-11-25
    相关资源
    最近更新 更多