如何根据单元格的值使单元格颜色RGB值发生变化? (在Excel中)

问题描述 投票:1回答:1

我是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
excel colors cell rgb
1个回答
0
投票

不要使用ColorIndexRGB,只需使用Color

 Cells(rwIndex, colIndex).Interior.Color = RGB(r, g, b)

当然,假设r, g, b都在(0,255)范围内

© www.soinside.com 2019 - 2024. All rights reserved.