9

I've come up with the following to alternate row colors within a specified range:

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 or remove
    End If
Next Cell

End Sub

That works, but is there a simpler method?

The following lines of code may be removed if your data contains no pre-exisiting colors:

    Else
        Cell.Interior.ColorIndex = xlNone

8 Answers 8

10

I need to do this frequently and like to be able to easily modify the colors I'm using for the banding. The following sub makes it very easy:

Sub GreenBarMe(rng As Range, firstColor As Long, secondColor As Long)
    rng.Interior.ColorIndex = xlNone
    rng.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"
    rng.FormatConditions(1).Interior.Color = firstColor
    rng.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)<>0"
    rng.FormatConditions(2).Interior.Color = secondColor
End Sub

Usage:

Sub TestGreenBarFormatting()
    Dim rng As Range
    Dim firstColor As Long
    Dim secondColor As Long

    Set rng = Range("A1:D12")
    firstColor = vbGreen
    secondColor = vbYellow

    Call GreenBarMe(rng, firstColor, secondColor)
End Sub
3
  • I get 'invalid procedure' error at line: rng.FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=0"
    – lfvv
    Dec 24, 2017 at 14:44
  • 1
    I solved it. My problem was that my Excel is in portuguese language, then I needed to translate the formula to portuguese equivalent. e.i "=MOD(ROW(),2)<>0" to ""=MOD(LIN();2)<>0"" ;)
    – lfvv
    Dec 24, 2017 at 16:49
  • Linha em vez de Row? Valeu! Dec 25, 2017 at 2:52
8

Alternating row colors can be done using conditional formatting:

screen capture

5
  • Thnaks James. Could you show us an example of what that would look like please? Also, does CF work in v2003?
    – Kurt
    Jan 7, 2011 at 19:23
  • 1
    Does the attached image not show up for you? I took a screen shot of the whole thing. It works in Excel 2000, so I assume that it will also work in Excel 2003
    – e.James
    Jan 7, 2011 at 19:23
  • Ah that explains it. I'm at work and our IT team seem to have an internet policy similar to that of North Korea: heavily censored. I'll have a look when I get home.
    – Kurt
    Jan 7, 2011 at 19:55
  • If I try this in Excel for Mac 2011 it returns an error telling my that my formula contains an error.
    – user1322720
    May 10, 2014 at 9:23
  • The problem with Conditional Formatting is that it's "super-volatile". Ever time you make a change on the sheet, it recalculates all the conditional cells even if they're not part of what was changed. Setting Conditional on every row of a big spreadsheet will slow you down big time! Jun 18, 2019 at 1:08
4

I needed a macro that would color every second row in a range, using only those rows that were visible. This is what I came up with. You don't have to loop through the rows.

Sub Color_Alt_Rows(Rng As Range)
    Application.ScreenUpdating = False

    Rng.Interior.ColorIndex = xlNone
    Rng = Rng.SpecialCells(xlCellTypeVisible)
    Rng.FormatConditions.Add Type:=xlExpression, Formula1:="=mod(row()+1,2)"
    Rng.FormatConditions(1).Interior.ColorIndex = 34
End Sub

Try it out with Color_Alt_Rows Range("a2:d5")

2

My Solution

A subroutine to assign to a button or some code

Public Sub Band_Goals()
    'Just pass the start and end rows
    'You will have to update the function to select the
    'the correct columns

    BandRows_Invisble 12, 144

End Sub

The Function

Private Sub BandRows_Invisble(StartRow As Integer, EndRow As Integer)

    Dim i As Long, nothidden As Boolean


    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    Range("A" & StartRow & ":K" & EndRow).Interior.ColorIndex = xlNone

    For i = StartRow To EndRow
        If Not Rows(i).Hidden Then
            nothidden = nothidden + 1
            If Not nothidden Then
                    'Download this app to help with color picking
                    'http://www.iconico.com/download.aspx?app=ColorPic
                    Range("A" & i & ":K" & i).Interior.Color = RGB(196, 189, 151)

            End If
        End If
    Next i

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
1
'--- Alternate Row color, only non-hidden rows count

Sub Test()

Dim iNumOfRows As Integer, iStartFromRow As Integer, iCount As Integer
iNumOfRows = Range("D61").End(xlDown).Row '--- counts Rows down starting from D61

For iStartFromRow = 61 To iNumOfRows

    If Rows(iStartFromRow).Hidden = False Then '--- only non-hidden rows matter

        iCount = iCount + 1

        If iCount - 2 * Int(iCount / 2) = 0 Then
            Rows(iStartFromRow).Interior.Color = RGB(220, 230, 241)
        Else
            Rows(iStartFromRow).Interior.Color = RGB(184, 204, 228)
        End If

    End If
Next iStartFromRow

End Sub
0

Well, you can delete the else part, since you will leave it in the default color

1
  • Well pointed out. I needed that for my own project as the data arrives with alternate row colors and I have to resort it based on values. The else cleans everything up for me, but totally appropriate to remove it otherwise. Thanks Chuck.
    – Kurt
    Jan 7, 2011 at 19:17
0

In my Excel 2010, there is an option to format as table, where you can also select a range and headers. No need for scripting. Excel Table

3
  • i think you missed the point of the question
    – lalachka
    Oct 9, 2022 at 4:19
  • @lalachka I think you missed the point of the answer. It offers tables with alternating row colors. How does that not answer the question?
    – Bernhard
    Oct 9, 2022 at 10:15
  • The range changes, thus the need for automation.
    – lalachka
    Oct 10, 2022 at 18:11
0

set these up initialized somewhere:

Dim arr_Lng_Row_Color(1) As Long
arr_Lng_Row_Color(0) = RGB(int_Color_1_R, int_Color_1_G, int_Color_1_B)
arr_Lng_Row_Color(1) = RGB(int_Color_2_R, int_Color_2_G, int_Color_2_B)

On any row you wish this will set the color

ws_SomeSheet.Rows(int_Target_Row).EntireRow.Interior.Color = arr_Lng_Row_Color(int_Target_Row Mod 2)

1
  • Not much different from what he already did
    – West
    Jul 18, 2022 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.