Toggle color in before double click event

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Toggle color in before double click event

Post by YasserKhalil »

Hello everyone

I am trying the following code

Code: Select all

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.Address = "$P$4" Then
        Cancel = True
        Target.Interior.Color = Not Target.Interior.Color = vbCyan
    End If
End Sub
But it doesn't work as expected. Is there an easier way to toggle the color without using the IF statement

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Toggle color in before double click event

Post by YasserKhalil »

I tried this way and worked but I welcome any more ideas

Code: Select all

        With Target.Interior
            .Color = IIf(.Color = xlNone Xor .Color = 16777215, vbCyan, xlNone)
        End With

User avatar
HansV
Administrator
Posts: 78474
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Toggle color in before double click event

Post by HansV »

In my opinion, using If ... Else ... End If is easier to understand than IIf.
And since you won't be double-clicking millions of times per second, it doesn't harm performance in any way.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Toggle color in before double click event

Post by YasserKhalil »

Thanks a lot, my tutor. It is just searching for new techniques.