Hey everyone,
I have just started learning VBA and I was practicing creating some very simple sub procedures. The procedure below inserts random numbers in a range that is 5 cells wide and 5 cells long:
Now here's what's driving me crazy. If I say Next r before Next c like in the next code, I get "Invalid Next control variable reference".
I was trying to figure out why this error occurs.
It appears that the variable order must be different in the For statement and the Next statement.
I have no idea why this is happening. I would really appreciate if someone could explain. Thanks!
I have just started learning VBA and I was practicing creating some very simple sub procedures. The procedure below inserts random numbers in a range that is 5 cells wide and 5 cells long:
Code:
Sub RandomCodeThatWorks()
Dim c As Double
Dim r As Double
For r = 1 To 5
For c = 1 To 5
Cells(r, c) = Rnd
[B][COLOR=DarkGreen] Next c
Next r[/COLOR][/B]
End Sub
Code:
Sub RandomCodeDoesNotWork()
Dim c As Double
Dim r As Double
For r = 1 To 5
For c = 1 To 5
Cells(r, c) = Rnd
[B] [COLOR=DarkRed] Next r
Next c[/COLOR][/B]
End Sub
It appears that the variable order must be different in the For statement and the Next statement.
I have no idea why this is happening. I would really appreciate if someone could explain. Thanks!