VBA Double Click to change color - Font and Fill

JDCline

New Member
Joined
Dec 3, 2019
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Self-taught Excel, have used this site for years to learn from all of you, and typically have been able to find answers in the past by reading other peoples experiences.

I have hit a wall however, as I know nothing about VBA, and there is a need to delve into this world, and have not been able to find what I am looking for.

The need is to double click a cell to change color, the contents of the cell are basically menu items, double clicking 'selects' the item.

Initially the text will be gray font. When double-clicked, the fill will change color (cyan), and the text changed to black. When double clicked again, it would revert to gray text and no fill.

We found the following worked to change the cell color.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = IIf(Target.Interior.ColorIndex >= 20, -4142, 20)
End Sub

I took a chance and added the second line, but it changes the font color only after double clicking several times, and does not revert the font back to gray.

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Interior.ColorIndex = IIf(Target.Interior.ColorIndex >= 20, -4142, 20)
Target.Font.ColorIndex = IIf(Target.Interior.ColorIndex >= 0, -4142, 0)
End Sub

What I believe is that I need an And statement somehow, and then revert to gray after double clicking to turn it off.

Is this something you guys can help with?

Thanks a bunch!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
How about
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Font.ColorIndex = IIf(Target.Interior.ColorIndex = 20, 16, 0)
Target.Interior.ColorIndex = IIf(Target.Interior.ColorIndex = 20, -4142, 20)
End Sub
 
Upvote 0
Thank-you so much! That seems to work exactly how I was hoping, much appreciated!!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,025
Members
449,060
Latest member
LinusJE

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