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 @   jetz  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
历史上的今天:
2009-02-22 Nod32的内网升级方案
2008-02-22 专注是力量的秘诀-注意力的分配
2007-02-22 关于vb中的容器
点击右上角即可分享
微信分享提示