excel VBA实现隔行着色

     
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值