VBA运行时错误424需要对象
这是我第一次使用VBA,我想看看我在这里做错了什么。有人知道我为什么会犯这个错误吗VBA运行时错误424需要对象,vba,Vba,这是我第一次使用VBA,我想看看我在这里做错了什么。有人知道我为什么会犯这个错误吗 Sub CountHighSales() Dim i As Integer Dim j As Integer Dim nHigh As Integer Dim cutoff As Currency cutoff = InputBox("What sales value do you want to check for?") For j = 1 To 6
Sub CountHighSales()
Dim i As Integer
Dim j As Integer
Dim nHigh As Integer
Dim cutoff As Currency
cutoff = InputBox("What sales value do you want to check for?")
For j = 1 To 6
nHigh = 0
For i = 1 To 36
If wsData.Range("Sales").Cells(i, j) >= cutoff Then _
nHigh = nHigh + 1
Next i
MsgBox ("For region " & j & ", sales were above " & Format(cutoff, "$0,000") & " on " & nHigh & " of the 36 months.")
Next j
End Sub
请尝试以下(注释)代码:
选项显式
次级销售()
Dim nHigh作为整数
作为货币
将wsData设置为工作表
Dim j尽可能长
设置wsData=Worksheets(“MyData”)'您在哪里定义了“wsData”?wsData
这是您的工作表的名称还是它的代码名?一旦定义了“wsData”,并假设它的“Sales”命名范围有36行6列,那么您就可以避免循环,只需编写:nHigh=WorksheetFunction.CountIf(wsData.range(“Sales”),“>=”和cutoff)我将代码重新写入:Sub CountHighSales()Dim nHigh As Integer Dim cutoff As Currency cutoff=InputBox(“您想检查什么销售值?”)nHigh=WorksheetFunction.CountIf(wsData.Range(“sales”),“>=”和cutoff)MsgBox(“对于地区”&j&“,销售高于”&Format(cutoff),“$0000”)和“在36个月内的”&nHigh和“.”结束Sub,但仍不工作。您在哪一行上出现错误?当您出现错误消息弹出窗口时,单击“调试”按钮,让我们知道哪一行以黄色突出显示。@MaryAnnRodriguez,如果此答案解决了您的问题,请单击答案旁边的复选标记将其切换为已接受om变灰了,请填写。谢谢
Option Explicit
Sub CountHighSales()
Dim nHigh As Integer
Dim cutoff As Currency
Dim wsData As Worksheet
Dim j As Long
Set wsData = Worksheets("MyData") '<--| set your data worksheet (change "MyData" to your actual worksheet with data name)
cutoff = Application.InputBox("What sales value do you want to check for?", "cutoff value", , , , , , 1) '<--| use 'Application.InputBox()' method with 'Type' parameter set to 1 to force a "numeric" input
With wsData.Range("Sales") '<--| reference the named range of given worksheet
For j = 1 To .columns.Count '<--| loop through referenced range columns
nHigh = WorksheetFunction.CountIf(.columns(j), ">=" & cutoff) '<--| count current column cells above cutoff value
MsgBox ("For region " & j & ", sales were above " & Format(cutoff, "$0,000") & " on " & nHigh & " of the 36 months.")
Next j
End With
End Sub