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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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