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:

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,761
Office Version
2013
Platform
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
16,761
Office Version
2013
Platform
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
16,761
Office Version
2013
Platform
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
16,761
Office Version
2013
Platform
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,099,377
Messages
5,468,256
Members
406,575
Latest member
Joe00

This Week's Hot Topics

Top