Background Interior color in VBA

How to Change the Color of Cell upon Selection?

In this article, you will learn how to change the color of the cell after selecting any color. We will use VBA code to change the color.

Question:-

I want to change the selected cell color to blue in range B2:E10 when cell is selected through VBA code.

Following is the snapshot of data we have:

 

img1

 

We need to follow the below steps:

  • Click on Developer tab
  • From Code group, select Visual Basic

 

img2

 

  • Enter the following code in the worksheet module

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Me.Cells.Interior.Color = xlNone

If Not (Intersect(Target, Range("B2:E10")) Is Nothing) Then

    ActiveCell.Interior.Color = 15651769

End If

End Sub

 

img3

 

  • As we select any cell in the range B2:E10, the selected cell will be highlighted in blue color.

 

img4

 

  • If we select any other cell then that cell will be highlighted.

 

img5

 

In this way, we can fill color in a cell in vba.

Leave a Reply

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

Terms and Conditions of use

The applications/code on this site are distributed as is and without warranties or liability. In no event shall the owner of the copyrights, or the authors of the applications/code be liable for any loss of profit, any problems or any damage resulting from the use or evaluation of the applications/code.