基于拆分计数,我需要用不同的颜色为行着色

时间:2018-07-13 15:37:39

标签: excel vba excel-vba

Sub test()

Dim MyCell As Range, i As Long

Dim SelectedRange As Range
Set SelectedRange = Application.InputBox("Select Range", Type:=8)

Dim SplitRow As Long
SplitRow = Application.InputBox("Split Row Num", Type:=1)

Dim FormatRange As Long
FormatRange = SelectedRange.Rows.Count / SplitRow

Application.ScreenUpdating = False
    For Each MyCell In SelectedRange
        If i < FormatRange Then
            MyCell.Interior.Color = vbRed
            i = i + 1
        Else
            MyCell.Interior.Color = vbYellow
        End If
    Next MyCell
Application.ScreenUpdating = True

End Sub

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

Sub test()
    Dim selectedRange As Range, splitRow As Long, formatRange As Long, i As Long

    Set selectedRange = Application.InputBox("Select Range", Type:=8)
    splitRow = Application.InputBox("Split Row Num", Type:=1)

    Application.ScreenUpdating = False

    With selectedRange
        formatRange = .Rows.Count / splitRow
        For i = 1 To .Rows.Count Step formatRange
            .Cells(i, 1).Resize(formatRange, 1).Interior.ColorIndex = i / formatRange + 3
        Next i
    End With

    Application.ScreenUpdating = True

End Sub