We know how powerful Excel is for highlighting our data, and you are probably familiar with Conditional Formatting and its options.
In the example below, however, we will show how to highlight our cells by changing their background in the easiest way possible.
Change Background Color in Excel
To change the background colors of any cell (cell A1 for example), all we need to do is click on it and then go to Home Tab >> Font >> Font Settings (the little icon in the lower right part in the tab):
We can also click CTRL + SHIFT + F to achieve the same results. Once we do, we will have a pop-up window.
On the pop-up window, we will select the Fill tab and choose our color (in our case red color). We will have the preview set as well:
We will simply click OK and we will have our cell A1 colored in red:
There is also a lot easier solution for this issue.
We could simply choose the Fill color from the Home tab. We will do it for cell A3:
Clicking on a color will be enough for the task. We have a lot of variations of our colors, and we can choose them all by clicking on More Colors.
Change Background Color in Excel with VBA
There is also a very neat way to automatically change the background color of our cells if certain conditions are met.
To do this option, we need to write sheet-related code. What this means is that we need to open up our VBA module by clicking CTRL + F11, and then clicking on our sheet:
Once there, we will input the following code on the right-side window:
1 2 3 4 5 6 7 8 |
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim MyRange As Range Set MyRange = Range("B2:D10") If Not Intersect(Target, MyRange) Is Nothing Then Target.Interior.Color = rgbBlue Else End If End Sub |
This is how our code looks in our VBA:
You will notice that we selected Worksheet in the first dropdown and SelectionChange in the second one.
The first thing we do in our code is to declare and set our variable:
1 2 3 |
Dim MyRange As Range Set MyRange = Range("B2:D10") |
Then we declare that if we change the color of clicked cells (Target variable) for all the cells in our range B2:D10:
1 2 3 4 |
If Not Intersect(Target, MyRange) Is Nothing Then Target.Interior.Color = rgbBlue Else End If |
For all the other cells, we will not change a thing.
Now, if we go back to our sheet and click on any given cell in our range, we will see that their background color (Interior.Color in our code) will be changed. However, if we click anywhere else, nothing will happen:
For all the other cells, we will not change a thing.
Now, if we go back to our sheet and click on any given cell in our range, we will see that their background color (Interior.Color in our code) will be changed. However, if we click anywhere else, nothing will happen: