2017-03-13 3 views
0

Dies ist das erste Mal, dass ich VBA verwende und versuche herauszufinden, was ich hier falsch mache. Weiß jemand, warum ich diesen Fehler bekomme?VBA-Laufzeitfehler 424 Benötigtes Objekt

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 
+1

Wo haben Sie "wsData" definieren? – user3598756

+0

'wsData' ist das der Name Ihres Blattes oder dessen ** CodeName **? –

+0

Sobald Sie "wsData" definieren und davon ausgehen, dass der benannte Bereich "Sales" 36 Zeilen und 6 Spalten hat, können Sie die Schleife umgehen und einfach schreiben: nHigh = WorksheetFunction.CountIf (wsData.Range ("Sales"), "> = "& Cutoff) – user3598756

Antwort

0

versuchen, diese (kommentierte) Code:

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 
+0

@MaryAnnRodriguez, wenn diese Antwort Ihre Frage löst, dann markieren Sie sie als akzeptiert, indem Sie auf das Häkchen neben der Antwort klicken, um sie von ausgegraut auf ausgefüllt umzuschalten. Danke – user3598756

Verwandte Themen