ActiveX command Button

FootBallBat

Board Regular
Joined
Jan 26, 2012
Messages
162
Hello I am looking for a code that when the command button is clicked it will change "B1"
Private Sub CommandButton2_Click()
If "B1" = 1 then "B1" = 2
else
If "B1" = 2 then "B1" = 1

I realize this is not a code it's just and example of what I'm looking for.
Thank you
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,653
Office Version
  1. 2013
Platform
  1. Windows
Try this:
Code:
Private Sub CommandButton2_Click()
'Modified  4/11/2019  9:07:12 PM  EDT
If Range("B1").Value = 1 Then
    Range("B1").Value = 2
Else
If Range("B1").Value = 2 Then
    Range("B1").Value = 1
End If
End If
End Sub
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,653
Office Version
  1. 2013
Platform
  1. Windows
Or This:
Code:
Private Sub CommandButton2_Click()
'Modified  4/11/2019  9:15:23 PM  EDT
If Range("B1").Value = 1 Then Range("B1").Value = 2: Exit Sub
If Range("B1").Value = 2 Then Range("B1").Value = 1: Exit Sub
End Sub
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,653
Office Version
  1. 2013
Platform
  1. Windows
Or this:
Code:
Private Sub CommandButton2_Click()
'Modified  4/11/2019  9:23:41 PM  EDT
With Range("B1")
    Select Case Range("B1").Value
        Case 1
            .Value = 2
        Case 2
            .Value = 1
    End Select
End With
End Sub
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,653
Office Version
  1. 2013
Platform
  1. Windows
Or like this:
Code:
Private Sub CommandButton2_Click()
'Modified  4/11/2019  9:30:41 PM  EDT
With [B1]
    Select Case [B1]
        Case 1
            .Value = 2
        Case 2
            .Value = 1
    End Select
End With
End Sub
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,133,656
Messages
5,660,145
Members
418,553
Latest member
judithcatherine

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
Top