【问题标题】:Countif, range, and cells [duplicate]计数,范围和单元格[重复]
【发布时间】:2017-09-01 15:30:15
【问题描述】:

我在 VBA 中有以下代码返回 1004 错误:

e = WorksheetFunction.CountIf(Worksheets("TG Teams").Range(Cells(4, w), Cells(12, w)), Worksheets("Calcs").Range("B" & a))

e、w、a都是整数。

代码用于检查 Calcs 表中的名称是否也出现在 TG Teams 表中。范围可变的原因是 CountIf 需要每周检查不同的列。

任何想法如何解决我在运行时遇到的错误 1004?

【问题讨论】:

  • 您需要将父级分配给 ALL 范围对象:Worksheets("TG Teams").Range(Worksheets("TG Teams").Cells(4, w), Worksheets("TG Teams").Cells(12, w))

标签: vba excel


【解决方案1】:

您还需要完全限定(Cells(4, w), Cells(12, w)) 以及他们所在的工作表(“TG 团队”),使用With

With Worksheets("TG Teams")
    e = WorksheetFunction.CountIf(.Range(.Cells(4, w), .Cells(12, w)), Worksheets("Calcs").Range("B" & a))
End With

【讨论】:

  • 谢谢,这很好用。我会在几分钟内接受它
  • 为什么我觉得“您需要完全限定 Cells 调用”是 80​​% 的 VBA 运行时错误问题的答案?
猜你喜欢
  • 2014-12-11
  • 2021-03-18
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
相关资源
最近更新 更多