Change cell color in a range by one click

VeKa27

Board Regular
Joined
Sep 11, 2015
Messages
56
Hi all,

Need your assistance. Imagine a worksheet with 2 colored ranges. (Like B2:G7 and B10:G15 colored in yellow). Now I am looking for a code that changes the cell color to green when I once click on a cell within those ranges. When I click on another cell in that range, this one has to turn green and the other one has to turn yellow again. So on the worksheet in the 2 ranges are only 2 green cells (the last clicked one in each range) an all other cells in the ranges are yellow.
I tried to upload an example file but didn't work.
Hope someone can help.

Many thanks in advance
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hi VeKa,

You could try something like this:

You need to add this code in the ThisWorkbook module in the Visual Basic Editor (More info: https://www.excelwtf.com/learn/visual-basic-editor/)


Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    If Not Intersect(Target, Range("B2", "G7")) Is Nothing Then
        ActiveSheet.Range("B2:G7").Interior.Color = vbYellow
        Target.Interior.Color = vbGreen
    End If
    
    If Not Intersect(Target, Range("B10", "G15")) Is Nothing Then
        ActiveSheet.Range("B10:G15").Interior.Color = vbYellow
        Target.Interior.Color = vbGreen
    End If
End Sub

Hope that Helps
Caleeco
 
Upvote 0

Forum statistics

Threads
1,223,895
Messages
6,175,257
Members
452,625
Latest member
saadat28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top