Can you change a cell color just by clicking it?

donutboy

New Member
Joined
Aug 6, 2009
Messages
4
I would like to turn a group of cells red or green just by using mouse clicks. Click one time and it turns red, click it again it turns green. Is that possible?


If that is not possible I would like to turn them colors by entering "y" for yes = green and "n" for no = red.



Thanks you guys/gals:cool:
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
On click I am not sure about.

If you plan on entering Y or N, you can make the cell color change using Conditional Formatting.

Highlight the cell or cells you want to change color. Click on Format -> Conditional Formatting. Set Cell Value -> Equal -> Y (format the cell to the color red). Add another condition, Set Cell Value -> Equal -> N (format the cell to the color green).

That should add the colors if nobody can determine the color change on click.
 
Upvote 0
33yq4vd.jpg
 
Upvote 0
Hello and welcome to MrExcel.

Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = 3
End Sub

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = 4
End Sub

Press ALT + Q to return to your sheet. Double click a cell to turn it red or right click to turn it green.
 
Upvote 0
The code worked to change them to red but nothing to green. it also seems to be stuck on red once it turns red. Could there be a third click to turn it red white again?

my bad it is a right click to go green. GOT THAT!

can I do something to turn it back to white?
 
Last edited:
Upvote 0
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    Target.Interior.ColorIndex = IIf(Target.Interior.ColorIndex >= 3, -4142, 3)
End Sub
 
Upvote 0
The previous code worked for me but try this. Double click once for red, double click again for green and so on, right click to remove the color

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Select Case Target.Interior.ColorIndex
    Case xlNone, 4: Target.Interior.ColorIndex = 3
    Case Else: Target.Interior.ColorIndex = 4
End Select
End Sub

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = xlNone
End Sub
 
Upvote 0
Hi there, I have just tried the code to change colours, it works a treat. Can the code be altered to change from red to orange and then to green.
 
Upvote 0

Forum statistics

Threads
1,214,801
Messages
6,121,644
Members
449,045
Latest member
Marcus05

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