我是VBA的新手,并尝试过以下无法解决的问题。你能帮帮我解决吗?我正在尝试根据它的值使每个单元格具有不同的RGB颜色:
Sub ColorCells()
Dim rwIndex As Integer
Dim colIndex As Integer
Dim r As Integer
Dim g As Integer
Dim b As Integer
For rwIndex = 1 To 1000
For colIndex = 1 To 1000
r = Cells(rwIndex, colIndex).Value Mod 256
g = Cells(rwIndex, colIndex).Value \ 256 Mod 256
b = Cells(rwIndex, colIndex).Value \ 65536 Mod 256
Cells(rwIndex, colIndex).Interior.ColorIndex = RGB(r, g, b)
Next colIndex
Next rwIndex
End Sub
不要使用ColorIndex
和RGB
,只需使用Color
Cells(rwIndex, colIndex).Interior.Color = RGB(r, g, b)
当然,假设r, g, b
都在(0,255)
范围内