Change the colour of a cell based on a different cell select

pcwilson78

New Member
Joined
Nov 14, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have searched the forums for this and found an article with VBA code to change the colour of a cell based on a certain cell click with no problems. However, I need the cell to then clear the colour once another cell has been clicked

Example
Cell A4 is clicked (has focus)
Cell E4 changes to green

Cell A6 is clicked (has focus)
Cell E4 clears its colour
Cell E6 changes to green

Heres the VBA

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Sheets("Sheet1")
Select Case Target.Address
Case "$A$3"
.Range("R4").Interior.Color = RGB(0, 255, 255)
Case "$A$4"
.Range("Z13").Interior.Color = RGB(0, 255, 0)
Case "$A$5"
.Range("M8").Interior.Color = RGB(0, 255, 0)
End Select
End With
End Sub


Many thanks in advance
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi,
There may be a more comprehensive way of doing it but this is what I have come up with
you can try

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If ActiveCell.Address = "$A$3" Then
    Range("R4").Interior.Color = RGB(0, 255, 255)
Else
    Range("R4").Interior.Color = RGB(255, 255, 255)
End If

If ActiveCell.Address = "$A$4" Then
    Range("Z13").Interior.Color = RGB(0, 255, 0)
Else
    Range("Z13").Interior.Color = RGB(255, 255, 255)
End If

If ActiveCell.Address = "$A$5" Then
    Range("M8").Interior.Color = RGB(0, 255, 0)
Else
    Range("M8").Interior.Color = RGB(255, 255, 255)
End If

End Sub
 
Upvote 0
Solution
Hi,

I have searched the forums for this and found an article with VBA code to change the colour of a cell based on a certain cell click with no problems. However, I need the cell to then clear the colour once another cell has been clicked

Example
Cell A4 is clicked (has focus)
Cell E4 changes to green

Cell A6 is clicked (has focus)
Cell E4 clears its colour
Cell E6 changes to green

Heres the VBA

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Sheets("Sheet1")
Select Case Target.Address
Case "$A$3"
.Range("R4").Interior.Color = RGB(0, 255, 255)
Case "$A$4"
.Range("Z13").Interior.Color = RGB(0, 255, 0)
Case "$A$5"
.Range("M8").Interior.Color = RGB(0, 255, 0)
End Select
End With
End Sub


Many thanks in advance
That's as close as i need. Thank you very much ... now just need to populate with 250 cells !

:)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
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