If we want to change cell color by clicking on it, we can implement this via editing VBA code. Don’t be afraid of code editing, this article will show you some short VBA codes directly for changing cell color by double clicking/right clicking on it, you can just copy and paste them into VBA code editing pane. This article will also show you the VBA code for highlighting cells by select them.
Table of Contents
Method 1: Change Cell Value by Double Clicking on It
Step 1: On current visible worksheet, right click on sheet name tab to load Sheet management menu. Select View Code, Microsoft Visual Basic for Applications window pops up.
Or you can enter Microsoft Visual Basic for Applications window via Developer->Visual Basic. You can also press Alt + F11 keys simultaneously to open it.
Step 2: In Microsoft Visual Basic for Applications window, enter below code in pane:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Cancel = True Target.Interior.Color = vbBlue End Sub
Step 3: Save VBA code and quit Microsoft Visual Basic for Applications.
Step 4: Try to double click on a cell. Verify that cell background color is filled with blue.
Method 2: Change Cell Value by Right Clicking on It
Step 1: Enter below code in Microsoft Visual Basic for Applications:
Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean) Cancel = True Target.Interior.Color = vbGreen End Sub
See screenshot below:
Step 2: This time right click on a cell. Verify that cell is changed to green.
Method 3: Highlight Cells If Cells Are Selected
Step 1: Enter below code in Microsoft Visual Basic for Applications:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) With Target .Worksheet.Cells.FormatConditions.Delete .FormatConditions.Add xlExpression, , "TRUE" .FormatConditions(1).Interior.Color = vbYellow End With End Sub
See screenshot below:
Step 2: Select a range of cells. Verify that cells are filled with yellow.