Sub CountLarge()
Sheets("Data").Select
Dim myNum As Integer
Dim cell As Range
Dim rngFullRange As Range
Dim nLarge As Integer
Do
myNum = Application.InputBox("Enter a number")
If myNum = "" Then Exit Sub
Loop Until myNum > 0 And myNum < 210
With Range(“A1”)
Range(.Offset(1, 0), .End(xlDown).End(xlToRight)).Name =
“dynamicRange”
End With
Set rngFullRange = wsData.Range(“dynamicRange”)
For Each cell In rngFullRange
If cell.Value > myNum Then
nLarge = nLarge + cell.Value
End If
Next
MsgBox (nLarge)
End Sub
This is my code I need it to go through each value in my worksheet (data starts at A2 and spans the whole worksheet so I set a dynamic range) and adds up all the values greater than the user input and displays it in a message box. When I run the program I am able to enter a number but I don't get a message box at the end.
**Note: I've edited the code using your feedback, currently the error is in the line With Range("A1"). The range fits the worksheet so I'm confused as to why, the data starts at A2 and fills the rest of the sheet which is why I used this range.
debug.print rngFullRange.Address
before theFor Loop
, what does it say in the Immediate Window (Ctrl+G)? – ProfexMsgBox WorksheetFunction.SumIf(wsData.Cells, ">" & InputBox("Enter A Number"))
? – ChronocidalmyValue
defined? I think that should bemyNum
. UsingOption Explicit
will alert you to errors like this because it forces you to define variables. – MarcF8
. Does it get caught in a loop? Does it bypass anything? Also, technically, it looks like you're mixing"
and“
. Make sure all quotes are"
and not the other ones (such as you have arounddynamicRange
). I'm betting that's the reason for your error onWith Range("A1")
...because the double quote characters used aren't the correct ones for VBA. – BruceWayne