在excel建了一个宏,怎么改变单元格的颜色?

如题所述

在excel建了一个宏,按以下方法改变单元格的颜色:

Cells(4, 6).Offset(0, 1).Interior.Color = Cells(4, 6).Interior.Color

上面VBA执行后则将(4,6)单元格的颜色向右偏移一格填充(4,6)颜色

将(0,1)换成(0,-1)则是向左偏移填充颜色 。

执行下面的宏代码:

Sub a()

For i = 2 To 100 '假设有99行要涂色的数据

If Cells(i, 3) <> "" Then

Cells(i, 3).Offset(0, 1).Interior.Color = Cells(i, 3).Interior.Color '向右的一个单元格填充颜色;

Cells(i, 3).Offset(0, 2).Interior.Color = Cells(i, 3).Interior.Color '向右的第二个单元格填充颜色;

Cells(i, 3).Offset(0, -1).Interior.Color = Cells(i, 3).Interior.Color '向左的一个单元格填充颜色;

Cells(i, 3).Offset(0, -2).Interior.Color = Cells(i, 3).Interior.Color '向左的第二个单元格填充颜色;

End If;

Next;

End Sub。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-12
Cells(4, 6).Offset(0, 1).Interior.Color = Cells(4, 6).Interior.Color
上面VBA执行后则将(4,6)单元格的颜色向右偏移一格填充(4,6)颜色
将(0,1)换成(0,-1)则是向左偏移填充颜色

执行下面的宏代码

Sub a()
For i = 2 To 100 '假设有99行要涂色的数据
If Cells(i, 3) <> "" Then
Cells(i, 3).Offset(0, 1).Interior.Color = Cells(i, 3).Interior.Color '向右的一个单元格填充颜色
Cells(i, 3).Offset(0, 2).Interior.Color = Cells(i, 3).Interior.Color '向右的第二个单元格填充颜色
Cells(i, 3).Offset(0, -1).Interior.Color = Cells(i, 3).Interior.Color '向左的一个单元格填充颜色
Cells(i, 3).Offset(0, -2).Interior.Color = Cells(i, 3).Interior.Color '向左的第二个单元格填充颜色
End If
Next
End Sub
第2个回答  2018-02-02
多少个单元格也是一样的,把你的代码贴上来看下呀
一句话的事
Selection.Interior.Color = 5296274
或Selection.Interior.ColorIndex = 8
把后面的颜色改成你要的颜色就行了,不管你一个单元格还是多个单元格,你选几个运行宏,就把这几个单元格填充了颜色
相似回答