Alternate Row Colors in Range

前端 未结 8 1662
轻奢々
轻奢々 2020-12-09 13:13

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(         


        
8条回答
  •  余生分开走
    2020-12-09 13:16

    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
    

提交回复
热议问题