is it possible to have a code for "change cell color when clicking on it" in the way that it will not change already formated cells with another color, please?
After applying your code all my formating (colours) is away. Thank you. Veronika
This article will show you some methods of how to change cell color while clicking on a cell, and change the highlighted color while selecting a range of cells in Excel.
Change cell color when cell is clicked with VBA code
Change the highlighted color when cells are selected with VBA code
Highlight entire row and column of active cell with Kutools for Excel
Here, you can change the background color of a cell when double clicking it or right clicking on it with the following VBA code.
1. In the worksheet you will change the cell color when clicking on it, right click the sheet tab and click View Code from the right-clicking menu.
2. In the Microsoft Visual Basic for Applications window, please copy and paste the below VBA code into the Code window.
VBA: change cell color when clicking on it
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Target.Interior.Color = vbRed End Sub Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean) Target.Interior.Color = vbGreen End Sub
3. Press Alt + Q keys simultaneously to close the Microsoft Visual Basic for Applications window.
Then, when you double click a cell, it will be colored in red. And when you right click on a cell, it will be colored in green. See screenshot:
One click to highlight row and column of selected cell in Excel:
Kutools for Excel's Reading Layout utility helps you quickly highlight row and column of selected cell in Excel as the below demo shown.
Download and try it now! (
30-day free trail)
By default, when selecting a range of cells, the highlighted color is gray. If you want to make the selected range more standing out, you can change its highlighted color to the one you need. Please do as follows.
1. Right click the sheet tab you want to change the highlighted color of the selected range, and click View Code from the right-clicking menu.
2. In the Microsoft Visual Basic for Applications window, please copy and paste the below VBA code into the Code window.
VBA code: change the highlighted color of selected range
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
3. Press Alt + Q keys at the same time to close the Microsoft Visual Basic for Applications window.
From now on, the highlighted color of selected range or cell is changed to yellow. When you click another cell or range of cells, the cells will be colored automatically.
If you are dealing with a large worksheet, it is necessary to highlight the entire row and column of the active cell in order to make reading more easily. Here the Reading Layout utility of Kutools for Excel can help you.
Before applying Kutools for Excel, please download and install it firstly.
1. Click Kutools > Reading Layout. See screenshot:
Then the reading layout is enabled, you can see the row and column of active cell are highlighted immediately.
Note: You can change the reading layout settings based on your needs as below screenshot shown.
If you want to have a free trial ( 30-day) of this utility, please click to download it, and then go to apply the operation according above steps.