Change background color of a cell when the cell is selected

bluefeather8989

Active Member
Joined
Nov 20, 2009
Messages
331
Office Version
  1. 365
Platform
  1. Windows
C4:Q5 if A CELL IN THIS RANGE is selected then background color changes to yellow (I may change this later). If I select another cell then the previous cell will return to background color (black). The new selected cell in the range background color changes to yellow.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
try below code

VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    ' Define the range to monitor
    Dim rngMonitor As Range
    Set rngMonitor = Me.Range("C4:Q5")
    
    ' Check if the selected cell is within the monitored range
    If Not Intersect(Target, rngMonitor) Is Nothing Then
        ' Reset background color for all cells in the monitored range
        rngMonitor.Interior.Color = RGB(0, 0, 0) ' Change to black
        
        ' Set the background color of the selected cell to yellow
        Target.Interior.Color = RGB(255, 255, 0) ' Change to yellow
    End If
End Sub
 
Upvote 0
Try the code below in the worksheet module if you don't have other conditional formatting


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("C4:Q5")) Is Nothing Then
      
        If Target.CountLarge = 1 Then
            Application.EnableEvents = False
          
            With Target
                Range("C4:Q5").FormatConditions.Delete
                .FormatConditions.Add xlExpression, , "TRUE"
                .FormatConditions(1).Interior.Color = vbYellow
            End With
      
        End If
    Else
        Range("C4:Q5").FormatConditions.Delete
  
    End If
  
    Application.EnableEvents = True
End Sub
 
Upvote 0
Try the code below in the worksheet module if you don't have other conditional formatting


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("C4:Q5")) Is Nothing Then
     
        If Target.CountLarge = 1 Then
            Application.EnableEvents = False
         
            With Target
                Range("C4:Q5").FormatConditions.Delete
                .FormatConditions.Add xlExpression, , "TRUE"
                .FormatConditions(1).Interior.Color = vbYellow
            End With
     
        End If
    Else
        Range("C4:Q5").FormatConditions.Delete
 
    End If
 
    Application.EnableEvents = True
End Sub
how would you do conditional formatting for this?
 
Upvote 0
It is conditional formatting triggered by VBA (it needs the VBA to work).
The condition is
1710702626582.png
 
Upvote 0
It is conditional formatting triggered by VBA (it needs the VBA to work).
The condition is
View attachment 108503
Try the code below in the worksheet module if you don't have other conditional formatting


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("C4:Q5")) Is Nothing Then
     
        If Target.CountLarge = 1 Then
            Application.EnableEvents = False
         
            With Target
                Range("C4:Q5").FormatConditions.Delete
                .FormatConditions.Add xlExpression, , "TRUE"
                .FormatConditions(1).Interior.Color = vbYellow
            End With
     
        End If
    Else
        Range("C4:Q5").FormatConditions.Delete
 
    End If
 
    Application.EnableEvents = True
End Sub
FOR SOME REASON THIS CODE WONT work in my workbook. it will work if i put it in a new blank workbook.(forgot the caps were on)
 
Upvote 0
In the Immediate window paste
VBA Code:
Application.EnableEvents = True
and press Enter

Then see if the code works
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,048
Members
453,014
Latest member
Chris258

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