VBA Code to Change Cell Color

VBA Code To Change Cell Color

Excel supports more than 16 million colors in a cell; hence you should know how to set the exact color in a cell. To do this, you can use RGB (Red, Green, Blue) function in VBA to set the color of a cell. The function requires 3 numbers from 0 to 255 [e.g. RGB(234,232,98)]. Below is a practice code that changes cell colors to Green, Red and Blue.

Change a Cells Background Color

				
					'This function can be used to change the color of a cell
Public Sub ChangeCellColor()
    
    'Change cell color to green
    Sheet1.Range("C4").Interior.Color = RGB(0, 255, 0)
    
    'Change cell color to red
    Sheet1.Range("C5").Interior.Color = RGB(255, 0, 0)
    
    'Change cell color to blue
    Sheet1.Range("C6").Interior.Color = RGB(0, 0, 255)
    
End Sub
				
			

VBA Code To Change Cell Color ,follow below steps:

Open an Excel file

  • Press Alt+F11
  • Insert a Module (Insert>Module) from menu bar
  • Paste the code in the module
  • Now add a shape in Excel sheet
  • Give a name to the shape like ‘Change Cell Color’
  • Right click on the shape and select ‘Assign Macro…’
VBA Code to change cell color
  • Select ‘ChangeCellColor’ from the list and click on ‘Ok’ button
VBA Code to Change Cell Color
  • Done, click on the shape to change the cell colors
VBA Code to Change Cell Color

Download Practice File

You can also practice this through our practice files. Click on the below link to download the practice file.

Recommended Articles

Similar Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *