VBA - If Cell selected and a particular colour then msg box

hmltnangel

Active Member
Joined
Aug 25, 2010
Messages
290
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Looking for a piece of code that does the following.

If any cell in range V4:V37 is selected, and the cell colour is orange, then a message box pops up.

Searching found me a few potential results, but the issue seems to lie in the fact that the orange is a Conditional Format rule. I can write a piece that says if cell selected then msg box, but its the only if its orange part that im stuck on.

Any suggestions?
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Place the following VBA code in the Sheet module:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

'   Exit if multiple cells selected at once
    If Target.CountLarge > 1 Then Exit Sub
    
'   Exit if cell selected is outside of range V4:V37
    If Intersect(Target, Range("V4:V37")) Is Nothing Then Exit Sub
    
'   See if color of selected cell is orange
    If Target.Interior.ThemeColor = xlThemeColorAccent2 Then
'    If Target.Interior.Color = 39423 Then
        MsgBox "Cell is orange!"
    End If
    
End Sub
Note that you will have to identify which color orange you are using, and adjust the code accordingly.
One way to find out is to turn on the Macro Recorder, and record yourself coloring a cell the color you want to identify.
Once you have it, you can plug in into the code.
Note I commented out a line. Depending on the color you choose, it could be a "Theme Color", or just color code.
 
Upvote 0
So if my colour is

With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.399975585192419
.PatternTintAndShade = 0

do I need to put that theme colour in anywhere?
 
Upvote 0
The code I gave you should work then, as-is, as I already coded it for "xlThemeColorAccent2".
Did you try it yet and see if it works?
 
Upvote 0
I thought that Joe.

I popped this in the correct worksheet code. Then when I select any cell thats that shade, nothing happens
 
Upvote 0
Try temporarily updating the code to this, and then select a cell with that orange coloring and tell me what number the MsgBox returns:
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    MsgBox Target.Interior.Color

'   Exit if multiple cells selected at once
    If Target.CountLarge > 1 Then Exit Sub
    
'   Exit if cell selected is outside of range V4:V37
    If Intersect(Target, Range("V4:V37")) Is Nothing Then Exit Sub
    
'   See if color of selected cell is orange
    If Target.Interior.ThemeColor = xlThemeColorAccent2 Then
'    If Target.Interior.Color = 39423 Then
        MsgBox "Cell is orange!"
    End If
    
End Sub
 
Upvote 0
Any cell in the range comes up with 16777215 regardless if its orange or not
 
Upvote 0
I misunderstood your original question. I thought you were saying the code you found was for Conditional Formatting. I now see that your color is being assigned by Conditional Formatting.

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

    MsgBox Target.DisplayFormat.Interior.Color

'   Exit if multiple cells selected at once
    If Target.CountLarge > 1 Then Exit Sub
    
'   Exit if cell selected is outside of range V4:V37
    If Intersect(Target, Range("V4:V37")) Is Nothing Then Exit Sub
    
'   See if color of selected cell is orange
    If Target.DisplayFormat.Interior.Color = 15773696 Then
        MsgBox "Cell is orange!"
    End If
    
End Sub
Whatever number pops up, replace the "15773696" number in my code with that value, and then you can get rid of that temporary MsgBox at the very top.
 
Upvote 0
Solution
Getting closer Joe, thanks :)

Its a table with banded rows, it works on one shade of the banded rows, but not the other. I did try various connotations of "elseif", or "or", but cant quite get it right
 
Upvote 0
Its a table with banded rows, it works on one shade of the banded rows, but not the other.
So you are wanting to check two different colors? You did not mention this is any of your previous posts!
Please be sure to include all important details here, we are not mind readers.

What are the color numbers for the two different codes you are checking for?
Please post the code that you tried to check for the both of these.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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