excel VBA实现隔行着色

来源:互联网 发布:可口可乐销量数据 编辑:程序博客网 时间:2024/06/06 07:20

     A    A    A    B    B    C    C    C    C    D    D    E    E    Sub zhuose()
Dim worksheet1 As Worksheet
Dim myRange As Range
Dim tempStr As String, cellStr As String, tempIndex As Integer, tempColorIndex As Integer, startIndex As Integer, endIndex As Integer
Set worksheet1 = ThisWorkbook.Worksheets("Sheet1")
tempIndex = 2
startIndex = 2
tempColorIndex = 6
j = 0
While worksheet1.Cells(tempIndex, 1) <> ""


   If cellStr <> worksheet1.Cells(tempIndex, 1) And cellStr <> "" Then
        
        ' 判断程序进入if条件的次数 每进一次if即换一次组  要对颜色进行一次变化
         j = j + 1
        If j Mod 2 = 0 Then
             tempColorIndex = 6
       Else
            tempColorIndex = 7
        End If
        
        endIndex = tempIndex - 1 ' 一组内组后一条的下标
        For i = startIndex To endIndex ' 同一组进行着色 即换组的时候
             worksheet1.Range("A" & i & ":E" & i).Interior.ColorIndex = tempColorIndex
         Next i
         startIndex = endIndex + 1 ' 一组数据开始的下标
    End If
    
    cellStr = worksheet1.Cells(tempIndex, 1) ' 中间标量用于判断数据是否换组
    tempIndex = tempIndex + 1
Wend


    ' 最后一组数据的处理
     j = j + 1 ' 判断程序进入if条件的次数
     If j Mod 2 = 0 Then
          tempColorIndex = 6
    Else
         tempColorIndex = 7
    End If
     
    ' 最后一组数据的着色
    For i = startIndex To tempIndex - 1
         worksheet1.Range("A" & i & ":E" & i).Interior.ColorIndex = tempColorIndex
    Next i
End Sub

原创粉丝点击