for next loop

  • Thread starter Thread starter LenC
  • Start date Start date
L

LenC

can someone help with this please............
I have wrote this small code and it wont work.

Range("m13").Name = "dat"
Range("bl14").Name = "res"
Dim colu
Dim rower
For colu = 0 To 48
For rower = 0 To -10

If Range("dat").Offset(rower, colu) = "X" Then
Range("res").Select
Range("res").Offset(0, colu) = rower * 2
End If
Next colu

If Range("dat").Offset(rower, colu) = "" Then
Range("res").Offset(0, colu) = rower * 2
End If
Next rower

End Sub

Because it stops and next colu is highlighted with the error message
COMPILE ERROR
INVALID NEXT CONTROL VARIABLE REFERENCE
I cant see whats wrong with it so I hope one of the subscribers can
tell me the problem.
Thanks in anticipation
Len
 
I'm not quite sure what this does, but you've got a couple problems.

For rower = 0 To -10
makes more sense as
For rower = 0 To -10 step -1
(or it'll stop as soon as rower is bigger than -10 (which means right away).

And when you do nested for/next loops, you want to match them in pairs.
Work from the outside in

for i = ...
for j = ...
for k = ...
....
next k
next j
next i

This compiled and ran for me, but I'm not sure if it's what you want:

Option Explicit
Sub testme03()
Dim colu As Long
Dim rower As Long

Range("m13").Name = "dat"
Range("bl14").Name = "res"

For colu = 0 To 48
For rower = 0 To -10 Step -1
If Range("dat").Offset(rower, colu) = "X" Then
'Range("res").Select 'Why select this?
Range("res").Offset(0, colu) = rower * 2
End If

If Range("dat").Offset(rower, colu) = "" Then
Range("res").Offset(0, colu) = rower * 2
End If
Next rower
Next colu
End Sub
 
Back
Top