VBA – Excel's Color Scheme
Change Color of Cell – .Interior.ColorIndex
To change the colour of a cell we can use:
Cell.Interior.ColorIndex = Num
Where:
• Cell is the cell reference
• Interior – refers to the colour of the actual cell colour (The interior property)
• Colourindex is a value between 1 and 56 for one of Excel’s 56 predefined colours
And Num is the number colour assigned to the cell. However, it isn’t always easy to remember which number represents which colour. The following subroutine changes the cell colour based on the row number. So for example row 3 will have colour 3 etc.
As there are 56 preset colours in Excel, this means that cells 59, 115 will have the same colour as the cell in row 3:
1 2 3 4 5 6 7 8 9 10 11 12 |
Option Explicit Private Sub CommandButton1_Click() Colour_Range (Sheets("Sheet2").Range("A1:A2000")) End Sub Sub Colour_Range(Cell_Range As Range) ' Will Colour each cell in range Dim Cell For Each Cell In Cell_Range Cell.Interior.ColorIndex = Cell.Row Mod 56 Cell.Offset(0, 0).Value = Cell.Row Next End Sub |
The routine is activated by a click event.
To download the .XLSM file for this tutorial, please click here.
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
Did you find this VBA tutorial useful? Then share it with your friends and colleagues: