Forum Discussion

mightymurph's avatar
mightymurph
Copper Contributor
May 23, 2021
Solved

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 "...
  • Rajesh_Sinha's avatar
    May 23, 2021

    mightymurph 

     

    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.

Resources