This program creates a table of numbers, and then attempts to sum it up, row by row. I’m using IsBlank() to test if the topmost cell is blank. If it is blank, it should end the loop, but if it isn’t, the loop should keep going. However, it keeps ending after the first time through the loop. Why is that?
I have a feeling it’s really obvious.
Edit: I should note that the whole “counter” thing is in there because I was going to start playing around with that if this worked. And it didn’t work, so I’m here!
Option Explicit
Dim Counter As Long
Dim i As Long
Dim col As Long
Dim row As Long
Sub SumRange()
For col = 1 To 8
For row = 1 To 6
Cells(row, col) = Rnd
Next row
Next col
Counter = 6
For i = 1 To 9
If IsEmpty(Cells(1, i)) = False Then
Cells(Counter + 1, i) = Application.WorksheetFunction.Sum(Range(Cells(1, i), Cells(Counter, i)))
Else
End If
End
Next
MsgBox Cells(4, 5)
End Sub
There are two problems:
The
Endstatement is incorrect. If I remember correctly,Endmeans to end the program. You have to explicitly state what you are ending (End If,End With, …). In this case you meanEnd If.You need to use
Exit Forto jump out of the for loop. I think you mean it to be where your currentEnd Ifstatement is.I’m not sure what you’re trying to do, but you can also consider using a while loop with the condition
While Not IsEmpty(Cells(1, i))and then increment the counterifrom within the loop. To me this feels a little better than a for loop with a jump in it.