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:
 
Try

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 3: Target.Interior.ColorIndex = 45
    Case 45: Target.Interior.ColorIndex = 4
    Case Else: Target.Interior.ColorIndex = xlNone
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

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This works great for what I need to do. However, I want to exclude some cells. Simply locking kills the subroutine and throws an error. Others will be using the form to enter their availability by coloring times they are available green. But they can't click on the headings or times in certain columns.
 
Upvote 0
This works awesome for me, but I am looking for a 4th color.
Ex:
Red
Orange
Blue
Green

When I return to my page after saving, it doesn't seem to work. Am I doing something wrong?

Thanks
 
Upvote 0
Can any body help me to get this function work: Code required for:
When I click on any cell then another required cell should be highlighted during that clicked cell.
 
Upvote 0
Can I dedicate this function to a single column?

Honeycutt Excel Sheet Screen Capture3.jpg
 
Upvote 0
Try this:

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rng As Range
Cancel = True
Set rng = ActiveSheet.Range("A2:A300") '<--- change range
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, rng) Is Nothing Then Exit Sub
Select Case Target.Interior.ColorIndex
Case xlNone, 4: Target.Interior.ColorIndex = 3
Case 3: Target.Interior.ColorIndex = 45
Case 45: Target.Interior.ColorIndex = 5
Case 5: Target.Interior.ColorIndex = 4
Case Else: Target.Interior.ColorIndex = xlNone
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
Try this:

VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rng As Range
Cancel = True
Set rng = ActiveSheet.Range("A2:A300") '<--- change range
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, rng) Is Nothing Then Exit Sub
Select Case Target.Interior.ColorIndex
Case xlNone, 4: Target.Interior.ColorIndex = 3
Case 3: Target.Interior.ColorIndex = 45
Case 45: Target.Interior.ColorIndex = 5
Case 5: Target.Interior.ColorIndex = 4
Case Else: Target.Interior.ColorIndex = xlNone
End Select
End Sub

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = xlNone
End Sub
Wow! I could not have figured this out on my own if I tried. THANK YOU!
 
Upvote 0
1581214032394.png


A couple of relatively minor comments about this snippet of code.
  1. I would move the Cancel = True line as shown. If not, you are denying a user who has the setting "Allow editing directly in cells" the opportunity to use that feature for the entire worksheet. Moving as shown would still allow such editing everywhere except 'rng'

  2. The underlined line could be removed. If there are no merged cells in the worksheet then it is impossible to double-click more than 1 cell anyway. If you are dealing with merged cells then you probably do want the double-click event to apply to a double-clicked merged area.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,822
Members
449,190
Latest member
rscraig11

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