Show/hide command buttons based on textbox value

rhombus4

Well-known Member
Joined
May 26, 2010
Messages
586
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Have 10 command buttons and a text box. When I enter a value in textbox1 i want to show that number of command buttons

E.g if I enter 0, no command buttons will appear, if I enter 4, command buttons 1,2 ,3 and 4 will be visible and the others won't be.

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.
This assumes the CommadButtons are named CommandButton1, CommendButton2... CommandButton10.

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] TextBox1_Exit([color=darkblue]ByVal[/color] Cancel [color=darkblue]As[/color] MSForms.ReturnBoolean)
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color]
    [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] 10
        Me.Controls("CommandButton" & i).Visible = (i <= Val(TextBox1.Value))
    [color=darkblue]Next[/color] i
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Thanks, but unfortunately they have random names. Is there a way to do it with random names
 
Upvote 0
Change the names to suit. Put them in the order you want them made visible.

Code:
[color=darkblue]Private[/color] [color=darkblue]Sub[/color] TextBox1_Exit([color=darkblue]ByVal[/color] Cancel [color=darkblue]As[/color] MSForms.ReturnBoolean)
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color], strRandomName [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] 10
        strRandomName = Array([B]"BtnA", "B", "Bob", "Open", "Shut", "X", "Y", "Z", "Alpha", "Frog"[/B])(i - 1)
        Me.Controls(strRandomName).Visible = (i <= Val(TextBox1.Value))
    [color=darkblue]Next[/color] i
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Thanks the last one works :)

I did try the 1st version renaming some buttons commandbutton1, commandbutton2 etc but it didn't work kept saying 'Could Not find specified object
maybe because there are more command buttons on the userform, that do other things and I just wanted to target the 10.
 
Upvote 0
You're welcome.

Renaming the buttons should have worked. I suspect one may have had a typo or something like that.
 
Upvote 0

Forum statistics

Threads
1,223,155
Messages
6,170,403
Members
452,325
Latest member
BlahQz

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