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
Sub One_Or_Zero()
With [A1]
.Value = 1 - .Value
End With
End Sub
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
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?
Thak you very much , its work very good as toggle buttonPlace 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
It is exactly what I needed, thank you very muchWelcome 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?
Its work very good as toggle button, Thak you very muchNot 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
Glad we could help, and thanks for the feedback