Doubleclick Yes, No and Blank - VBA

Garri

New Member
Joined
May 28, 2019
Messages
3
Hello everyone,

I found a Code for VBA to use double click in certain cell to activate Yes and No. However, I cannot to add to that code that it also show blank (or returns to the default result aka do not show anything).

Can anyone help me please?

P.s I am newbie.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address <> "C1" Then Exit Sub
Cancel = True
With Target
If .Value = "Yes" Then
.Value = "No"
Else
.Value = "Yes"
End If
End With
End Sub
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,348
Office Version
  1. 365
Platform
  1. Windows
How about
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Target.Address(0, 0) <> "C1" Then Exit Sub
      Cancel = True
      With Target
      If .Value = "Yes" Then
         .Value = "No"
      ElseIf .Value = "No" Then
         .Value = ""
      Else
         .Value = "Yes"
      End If
   End With
End Sub
 

Eric W

MrExcel MVP
Joined
Aug 18, 2015
Messages
10,746
Try:

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

    If Target.Address <> "$C$1" Then Exit Sub
    Cancel = True
    
    Select Case Target.Value
        Case "Yes": Target.Value = "No"
        Case "No": Target.Value = ""
        Case Else: Target.Value = "Yes"
    End Select
    
End Sub
 
Last edited:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,858
Office Version
  1. 2010
Platform
  1. Windows
I'm late to the thread (as usual), but here is one more way to do this...
Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   If Target.Address(0, 0) = "C1" Then
     Cancel = True
     Target.Value = Choose(1 + InStr(" YN", Left(Target.Value, 1)), "", "Yes", "No")
   End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,127,167
Messages
5,623,123
Members
415,956
Latest member
Footballtend

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
Top