Button to toggle cell Value

neilsinc

Board Regular
Joined
Jan 20, 2014
Messages
135
I have a button on my sheet that I want to use to toggle a cell value between 1 and 0.

Any idea how I would do that with VBA?

i.e. if the value is 1 when the button is pressed, change it to 0 and vice versa

Thanks in advance
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try:-
Code:
Private Sub CommandButton1_Click()
With Range("A1")
    .Value = IIf(.Value = 1, 0, 1)
End With
End Sub
 
Upvote 0
Assuming we are toggling the value in Range ("A1") then use this script:

Code:
Sub Add_Value()
    If Range("A1").Value ="1" Then
        Range("A1").Value = "0"
            Else: Range("A1").Value = "1"
    End If
End Sub
 
Upvote 0
Try:-
Code:
Private Sub CommandButton1_Click()
With Range("A1")
    .Value = IIf(.Value = 1, 0, 1)
End With
End Sub
You can simplify while eliminating the slow IIf function at the same time...

Code:
Private Sub CommandButton1_Click()
  With Range("A1")
    .Value = 1 - .Value
  End With
End Sub

Although I would probably eliminate the With..End With block (not sure anything is gained by using it when there is only 2 references to its object)...

Code:
Private Sub CommandButton1_Click()
  Range("A1").Value = 1 - Range("A1").Value
End Sub
 
Last edited:
Upvote 0
If we are only toggling the value in one cell I do not see why speed needs to be considered.
You can simplify while eliminating the slow IIf function at the same time...

Code:
Private Sub CommandButton1_Click()
  With Range("A1")
    .Value = 1 - .Value
  End With
End Sub

Although I would probably eliminate the With..End With block (not sure anything is gained by using it when there is only 2 references to its object)...

Code:
Private Sub CommandButton1_Click()
  Range("A1").Value = 1 - Range("A1").Value
End Sub
 
Upvote 0

Forum statistics

Threads
1,226,730
Messages
6,192,699
Members
453,747
Latest member
tylerhyatt04

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