Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing font color in cell on double-click

Tags:

excel

vba

In Excel (using VBA) I want to change the color of the check marks from gray to green on double-click, then back to gray again on double-click (basically a toggle). Current code is below - NOTHING I have tried has worked. Please help.

Here is the image. The font is Wingdings, character code is 252, font colors are #BEBEBE and #008000. I want those gray check marks to change to green on double click and if double clicked again, back to gray. This is in a range of cells (B7 to C150).

screenshot

CODE:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

If Target.Column = 2 Then
Cancel = True
End If

If Target.Column = 3 Then
Cancel = True
End If

With Target.Font.Color
    If Not .Font.Color = vb15 Then
        .ColorIndex = vb10
    ElseIf Not .Font.Color = vb10 Then
        .ColorIndex = vb15
    End If
End With

End Sub

Thanks for any help!!

like image 546
twoawesomedogs Avatar asked Jan 31 '26 04:01

twoawesomedogs


1 Answers

This will work, if the color is red or blue initially:

Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    With Target
        If .Column <> 2 And .Column <> 3 Then Exit Sub
        Cancel = True

        If .Font.Color = vbBlue Then
            .Font.Color = vbRed
        ElseIf .Font.Color = vbRed Then
            .Font.Color = vbBlue
        End If
    End With    
End Sub

I could not find vb10 or vb15, I guess these are some font colors generated by you. The idea in the event is to Exit Sub, if you are not happy with the condition.


Even better, if you decided to use Select Case with default option:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    With Target
        If .Column <> 2 And .Column <> 3 Then Exit Sub
        Cancel = True

        Select Case .Font.Color
            Case vbBlue
                .Font.Color = vbRed
            Case vbRed
                .Font.Color = vbBlue
            Case Else
                .Font.Color = vbBlue
        End Select
    End With    
End Sub
like image 82
Vityata Avatar answered Feb 03 '26 09:02

Vityata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!