大众计算机学习网

专门提供各种电脑教程

利用VBA轻松实现Excel满足条件的行列更换背景颜色代码实例

Admin | 2008-4-14 20:08:51 | 被阅次数 | 13938

  如题所示:

  代码一实现满足条件的行改变背景颜色
-------------------------------------------------------

  Sub ChangeRowRGB '
   Dim i As Integer
   For i = 88 To 1 Step -1
       If Cells(i, 2) < 60 Then
           Sheets(j).Rows(i).Interior.ColorIndex = 20
       End If
   Next i
  End Sub

-------------------------------------------------------

  代码解释:ColorIndex = 20为颜色序号,可自行改变!

  代码二:实现满足条件的列改变背景颜色
-------------------------------------------------------

  Sub ChangeColumnsRGB'删除指定的单元格为空的行
  Dim i As Integer
   For i = 88 To 1 Step -1
       If Cells(i, 2) < 60 Then
           Sheets(1).Columns(i).Interior.ColorIndex = 10
       End If
   Next i
  End Sub

-------------------------------------------------------

  知识扩展:批量改变多表满足条件的行列背景颜色!

-------------------------------------------------------

 Sub ChangeRowAndColumnsRGB'
  Dim i As Integer
  For j = 1 To 6
   For i = 88 To 1 Step -1
    If Cells(i, 3) = >80 Then
     Sheets(j).Columns(i).Interior.ColorIndex = 6
     Sheets(j).Rows(i).Interior.ColorIndex = 8

    End If
   Next i
  Next j
 End Sub

-------------------------------------------------------