# 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
``````
LVL 4
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
When assigning colors to RGB() values use .Color rather than .ColorIndex as the property.

»bp
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Hardware Tester and DebuggerAuthor Commented:
Wow... That was fast... Haha Thank you :)

@Bill Prew How about if I want to use a code involving vbYellow? Any Idea, how to edit it? :)
0
Data ProcessorCommented:
You would replace the RGB(...) with the appropriate colour constant.
``````       If R Mod 2 = 0 Then
Cells(R, C).Interior.Color = vbWhite
Else

Cells(R, C).Interior.Color = vbYellow
End If
``````
0
Software & Systems EngineerCommented:
``````Public Function Alternate()
Dim C As Integer
Dim R As Integer
R = 1
Do While R < 20
For C = 1 To 20
If R Mod 2 = 0 Then
Cells(R, C).Interior.ColorIndex = 5
Else
Cells(R, C).Interior.ColorIndex = 50
End If
Next
R = R + 1
Loop
End Function
``````
0
Distinguished Expert - 2017Commented:
Is this Needs Help With Some Editing Of A Code doesn't solve your problem?
0
Hardware Tester and DebuggerAuthor Commented:
Hi! @Shums That other thread did solve my problem! However, I just needed help checking this one as I could't spot the mistake.. As you can see from the "Best Solution", it was a very minor mistake so forgive me as I am still a beginner hahaha :)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.