vba主动着色

'原来的条件格式效率太低,改为主动方式着色
Sub SetColor()
     On Error Resume Next
     Dim hang As Integer  '行数
     Dim lie As Integer
     Dim IsBuy As Boolean
     Dim IsSell As Boolean
     hang = ActiveSheet.UsedRange.Rows.Count
     With ActiveSheet
         lie = .Cells(2, .Columns.Count).End(xlToLeft).Column
     End With
     For i = 2 To hang
         IsBuy = False
         IsSell = False
         For j = 1 To lie
           If ActiveSheet.Cells(i, j).Value = "买" Then IsBuy = True
           If ActiveSheet.Cells(i, j).Value = "卖" Then IsSell = True
         Next
         If IsBuy Then
             ActiveSheet.Range(Cells(i, 1), Cells(i, lie)).Interior.Color = RGB(255, 153, 204)  '红
         Else
             If IsSell Then
                ActiveSheet.Range(Cells(i, 1), Cells(i, lie)).Interior.Color = RGB(204, 255, 204)  '绿
             Else
                 ActiveSheet.Range(Cells(i, 1), Cells(i, lie)).Interior.Color = xlNone
             End If
         End If
     Next i
End Sub

好处是自适应,表格里面任何位置只要有买或卖,就自动为该行着色

image

image

posted @ 2025-02-22 16:05  jetz  阅读(10)  评论(0)    收藏  举报