Forum Discussion
mightymurph
May 23, 2021Copper Contributor
basic command button Yes/No VBA code add text
Hello excel community, I am good with excel forumla's but I'm a beginner (less than) when it comes to VBA. I want to create 2 buttons. Yes and No (I can do this) Clickable, to input, "Yes" or "...
- May 23, 2021
This is the VBA code you can use for Yes & No.
Private Sub CommandButton1_Click() Dim lastrow As Long lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1 If CommandButton1.Caption = "Yes" Then ActiveSheet.Cells(lastrow, "A").Value = "Yes" CommandButton1.Caption = "No" ElseIf CommandButton1.Caption = "No" Then ActiveSheet.Cells(lastrow, "A").Value = "No" CommandButton1.Caption = "Yes" End If End Sub
N.B.
- After you Create Command Button, set it's CAPTION Yes (select button & Right Click then Hit Property ).
- Remember in Code I've use CAPTION as Yes & No and it's case sensitive.
- Both Column name as well Value to enter are editable.
- Save the WB as Macro Enabled *.xlsm.
Rajesh_Sinha
May 23, 2021Iron Contributor
This is the VBA code you can use for Yes & No.
Private Sub CommandButton1_Click()
Dim lastrow As Long
lastrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
If CommandButton1.Caption = "Yes" Then
ActiveSheet.Cells(lastrow, "A").Value = "Yes"
CommandButton1.Caption = "No"
ElseIf CommandButton1.Caption = "No" Then
ActiveSheet.Cells(lastrow, "A").Value = "No"
CommandButton1.Caption = "Yes"
End If
End Sub
N.B.
- After you Create Command Button, set it's CAPTION Yes (select button & Right Click then Hit Property ).
- Remember in Code I've use CAPTION as Yes & No and it's case sensitive.
- Both Column name as well Value to enter are editable.
- Save the WB as Macro Enabled *.xlsm.
mightymurph
May 23, 2021Copper Contributor
Rajesh_Sinha Thank You
- Rajesh_SinhaMay 23, 2021Iron ContributorGlad to help you,,, if it works for you the you may accept is as Best Answer as well Like too,, and keep asking ☺