Forum Discussion
ONG ZHEN YANG RP
May 18, 2018Brass Contributor
Needs Help Editing a Simple Code
I am creating a code that would make all odd rows yellow and all even rows white
It's not working properly right now so I would appreciate any help editing it! The error is that the subscript is out of range
Sub loops_exercise()
Dim R As Integer
R = 1
Do While R < 20
Dim C As Integer
For C = 1 To 20
If R Mod 2 = 0 Then
Cells(R, C).Interior.ColorIndex = RGB(255, 0, 0)
Else
Cells(R, C).Interior.ColorIndex = RGB(2, 0, 0)
End If
Next
R = R + 1
Loop
End Sub
- Haytham AmairahSilver Contributor
- Matt MickleBronze Contributor
This is a little bit simpler then what you have. Instead of looping through the cells individually one at a time you can just color the whole row at once.
Note: Column T is Column # 20
Sub loops_exercise() Dim R As Integer For R = 1 To 20 If R Mod 2 = 0 Then Range("A" & R & ":T" & R).Interior.Color = vbWhite Else Range("A" & R & ":T" & R).Interior.Color = vbYellow End If Next R End Sub