我使用下面的代码利用“上周应用”表格进行数据收集,并在同一张表中更新计数。
我创建了一个按钮,我试图通过按钮运行此代码。
执行时,我在行
中得到“需要对象”的错误[W5] = wf.CountIf(.Range("I:I"), "Trophy")
我的代码:
Sub Prevcount()
Sheets("Previous Week apps")
With ActiveWorkbook.Worksheets("Previous week apps")
[W5] = wf.CountIf(.Range("I:I"), "Trophy")
End With
With ActiveWorkbook.Worksheets("Previous week apps")
[W7] = wf.CountIfs(.Range("I:I"), "Trophy", .Range("E:E"), "COMPATIBLE")
End With
With ActiveWorkbook.Worksheets("Previous week apps")
[W9] = wf.CountIfs(.Range("I:I"), "Trophy", .Range("F:F"), "COMPATIBLE")
End With
With ActiveWorkbook.Worksheets("Previous week apps")
[W11] = wf.CountIfs(.Range("I:I"), "Trophy", .Range("Q:Q"), "UG")
End With
End With
End Sub
答案 0 :(得分:1)
尝试使用简短版本(将ActiveWorkbook
替换为ThisWorkbook
,其中包含您的代码):
Sub Prevcount()
With ThisWorkbook.Worksheets("Previous week apps")
.Range("W5").Value = WorksheetFunction.CountIf(.Range("I:I"), "Trophy")
.Range("W7").Value = WorksheetFunction.CountIfs(.Range("I:I"), "Trophy", .Range("E:E"), "COMPATIBLE")
.Range("W9").Value = WorksheetFunction.CountIfs(.Range("I:I"), "Trophy", .Range("F:F"), "COMPATIBLE")
.Range("W11").Value = WorksheetFunction.CountIfs(.Range("I:I"), "Trophy", .Range("Q:Q"), "UG")
End With
End Sub