1

I'm new to VBA and have tried the below which doesn't work. Could you help me fix it. I'm trying to make each cell have a different RGB colour based on it's value:

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
emma frost
  • 19
  • 1

1 Answers1

0

Don't use ColorIndex with RGB, just use Color

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

Assuming of course r, g, b are all in the (0,255) range

BruceWayne
  • 22,923
  • 15
  • 65
  • 110