在EXCEl中,用VBA实现单元格颜色改变
代码如下:
Sub ts()
Dim rg As Range
Dim r As Long
r = Columns("A:C").SpecialCells(xlLastCell).Row
For Each rg In Range("A1:C" & r)
If rg < Range("E" & rg.Row) Then
rg.Interior.Color = <a href="https://www.baidu.com/s?wd=RGB&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBPAc3m163uAF-P10sujmk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1Rdn1DYrH6zPHfznWRvrHbLn6" target="_blank" class="baidu-highlight">RGB</a>(255, 0, 0)
Else
rg.Interior.Color = <a href="https://www.baidu.com/s?wd=RGB&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBPAc3m163uAF-P10sujmk0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6K1TL0qnfK1TL0z5HD0IgF_5y9YIZ0lQzqlpA-bmyt8mh7GuZR8mvqVQL7dugPYpyq8Q1Rdn1DYrH6zPHfznWRvrHbLn6" target="_blank" class="baidu-highlight">RGB</a>(146, 208, 80)
End If
Next
End Sub
Whit Sheets["表名称"]
r=.Range["A65536"].End(xlup).Row
For i=1 to r
If .Cells(i,1) .Value<20 Then
.Cells(i,1).Font.ColorIndex=4
Eed If
If .Cells(i,1) .Value>19 And .Cells(i,1) .Value<41 Then
.Cells(i,1).Font.ColorIndex=6
End If
If .Cells(i,1) .Value>40 Then
.Cells(i,1).Font.ColorIndex=3
End If
Next i
End With