Two command buttons in same location in userform

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,731
Office Version
  1. 2007
Platform
  1. Windows
I have a userform which has 2 command buttons.
Depending on a value taken from a Textbox depends which command button is shown.

I would like to have both command buttons in the same place.

When I design my form it would mean one command button sits directly ontop the other.

But surely that isn’t the correct answer.
Any advice welcome.

As an example let’s call them commandbutton 1 & 2 should you supply a code etc.

Thanks.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
When I design my form it would mean one command button sits directly ontop the other.

But surely that isn’t the correct answer.
Why wouldn't that be the correct answer? Have you tried designing it yet?

You could also just use one command button which would perform different operations depending on the textbox value.
 
Upvote 0
One reason why I thought it would be wrong is later down the road when I’m making adjustments etc I wouldn’t expect to have one under the other so what I see is what I assume is there.

How would you have one button but two operations like you mention.
 
Upvote 0
Yes, that would be an issue forgetting the second button is present.

As for having two operations from a single button, something like this:

VBA Code:
Sub CommandButton1_Click()
If TextBox1.Value = "A" Then
'code here to perform operation for value A
Else
'code here to perform operation for value B
End If
End Sub

The above is a very basic idea, which would likely need to be made a bit more robust to account for values not equal to A or B. But that will depend on more specific information from you.
 
Upvote 0
Just thinking wouldn’t be able to have text on the button if just changing code.

I’m sure somewhere I’ve seen some code where REPLACE was used. You could then have different text on the button.
 
Upvote 0
Quick and dirty example.
Make a UserForm with a CommandButton and name this CommandButton "Cmd1" (without double quotation marks)
In Cell A1, enter a value of 6
Now open your UserForm and toggle the CommandButton.
Codes are as follows.
Code:
Private Sub Cmd1_Click()
    If Sheets("Sheet1").Range("A1").Value < 10 Then
        Sheets("Sheet1").Range("A1").Value = 15
            Me.Cmd1.Caption = "Lower"
                Else
            Sheets("Sheet1").Range("A1").Value = 5
        Me.Cmd1.Caption = "Higher"
    End If
End Sub

Code:
Private Sub UserForm_Initialize()
    If Sheets("Sheet1").Range("A1").Value < 10 Then Me.Cmd1.Caption = "Higher" Else Me.Cmd1.Caption = "Lower"
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,182
Members
452,615
Latest member
bogeys2birdies

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
Back
Top