Momentary Button to toggle cell Value

Sorin

New Member
Joined
Jan 22, 2024
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
Hello people

I need a button that works like "momentary button" to change the vallue in to a cell: when pressed it should be 1 and when released it should be 0.

Thanks in advance
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Place a button on your worksheet. Paste this code in a standard module. Right click to button, assign this macro to the button.

VBA Code:
Sub toggle_Value()
  With ActiveSheet
    If .Range("A1").Value = 1 Then
      .Range("A1").Value = 0
    Else
      .Range("A1").Value = 1
    End If
  End With
End Sub
 
Upvote 0
Not sure what you mean exactly by 'press & release' but if it's simply a button click then I think @Peter_SSs came up with a more succinct methodology recently (assumes your starting value in the cell in question is either 1 or 0/blank)
VBA Code:
Sub One_Or_Zero()
    With [A1]
        .Value = 1 - .Value
    End With
End Sub
 
Upvote 0
Welcome to the MrExcel board!

This is definitely not an area of strength of mine but this seems to do what I think you are asking.

Put an ActiveX Command Button on your worksheet (presumably near the cell you want the values to appear in - I used cell A1)
Put these code in that worksheet's code module. If you already have other ActiveX Command Buttons on the worksheet you would need to change the name in these codes.

VBA Code:
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  Range("A1").Value = 1
End Sub

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  Range("A1").Value = 0
End Sub

Now when I click & hold the button a 1 appears in A1 and when I release the mouse button the value changes to 0.
Here it is in action
1_0_Button.gif


Is that what you wanted?
 
Last edited:
Upvote 0
Welcome to the MrExcel board!

This is definitely not an area of strength of mine but this seems to do what I think you are asking.

Put an ActiveX Command Button on your worksheet (presumably near the cell you want the values to appear in - I used cell A1)
Put these code in that worksheet's code module. If you already have other ActiveX Command Buttons on the worksheet you would need to change the name in these codes.

VBA Code:
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  Range("A1").Value = 1
End Sub

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  Range("A1").Value = 0
End Sub

Now when I click & hold the button a 1 appears in A1 and when I release the mouse button the value changes to 0.
Here it is in action
View attachment 105549

Is that what you wanted?

Place a button on your worksheet. Paste this code in a standard module. Right click to button, assign this macro to the button.

VBA Code:
Sub toggle_Value()
  With ActiveSheet
    If .Range("A1").Value = 1 Then
      .Range("A1").Value = 0
    Else
      .Range("A1").Value = 1
    End If
  End With
End Sub
Thak you very much , its work very good as toggle button
 
Upvote 0
Welcome to the MrExcel board!

This is definitely not an area of strength of mine but this seems to do what I think you are asking.

Put an ActiveX Command Button on your worksheet (presumably near the cell you want the values to appear in - I used cell A1)
Put these code in that worksheet's code module. If you already have other ActiveX Command Buttons on the worksheet you would need to change the name in these codes.

VBA Code:
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  Range("A1").Value = 1
End Sub

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  Range("A1").Value = 0
End Sub

Now when I click & hold the button a 1 appears in A1 and when I release the mouse button the value changes to 0.
Here it is in action
View attachment 105549

Is that what you wanted?
It is exactly what I needed, thank you very much
 
Upvote 0
Not sure what you mean exactly by 'press & release' but if it's simply a button click then I think @Peter_SSs came up with a more succinct methodology recently (assumes your starting value in the cell in question is either 1 or 0/blank)
VBA Code:
Sub One_Or_Zero()
    With [A1]
        .Value = 1 - .Value
    End With
End Sub
Its work very good as toggle button, Thak you very much
 
Upvote 0

Forum statistics

Threads
1,215,389
Messages
6,124,665
Members
449,178
Latest member
Emilou

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