Excel VBA: Alternate Row Color in Range

Posted by Kurt on Stack Overflow See other posts from Stack Overflow or by Kurt
Published on 2011-01-07T18:47:53Z Indexed on 2011/01/07 18:54 UTC
Read the original article Hit count: 181

Filed under:
|
|

I spent a VERY long time today looking up a method to alternate row colors within a specified range. There really isn't a lot out there and to be honest what I found just looked over-complicated. So, I decided to stop acting like a shameless 'script-kiddy' and put the below sample together:

Sub AlternateRowColors()
Dim lastRow as Long

lastRow = Range("A1").End(xlDown).Row

For Each Cell In Range("A1:A" & lastRow) ''change range accordingly
    If Cell.Row Mod 2 = 1 Then ''highlights row 2,4,6 etc|= 0 highlights 1,3,5
        Cell.Interior.ColorIndex = 15 ''color to preference
    Else
        Cell.Interior.ColorIndex = xlNone ''color to preference
    End If
Next Cell

End Sub

Now I know that works, but I was wondering if there's a simpler method?

If so, please do tell because I'm very eager to learn simplification as I have a tendency to write verbose code at present. If not, then may this entry find it's way to page 1 of Google for it's search term(s), because it took me absolutely ages to find anything even remotely useful.


Comments left for script-kiddies' benefit.

© Stack Overflow or respective owner

Related posts about macros

Related posts about excel-vba