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

Open in new window

LVL 4
An Average Forum Participant Just For FunHardware Tester and DebuggerAsked:
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.

Bill PrewIT / Software Engineering ConsultantCommented:
When assigning colors to RGB() values use .Color rather than .ColorIndex as the property.


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
An Average Forum Participant Just For FunHardware 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? :)
NorieAnalyst Assistant Commented:
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

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

John TsioumprisSoftware & 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

Open in new window

ShumsExcel & VBA ExpertCommented:
Is this Needs Help With Some Editing Of A Code doesn't solve your problem?
An Average Forum Participant Just For FunHardware 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 :)
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.