Alternate Row Colors in Range

前端 未结 8 1625
轻奢々
轻奢々 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:15

    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)
    
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-09 13:19
    '--- 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 讨论(0)
  • 2020-12-09 13:20

    Alternating row colors can be done using conditional formatting:

    screen capture

    0 讨论(0)
  • 2020-12-09 13:20

    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")

    0 讨论(0)
  • 2020-12-09 13:26

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

    0 讨论(0)
提交回复
热议问题