Load Userform using ComboBox

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
When a user hits a button I am bringing up a small Userform with a ComboBox on it.

The values for that combobox (currently) are UserForm1 and UserForm2.

What I am trying to do is, when the user selects the value from the combobox, that value is passed into the load userform command, but it's erroring out on me.

This is the code I am trying to use, but it's not working. Can anyone assist?

Code:
Sub ComboBox1_Change()
Dim sFormName As String

sFormName = UserForm3.ComboBox1.Value

    Load sFormName
    sFormName.Show
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
The show method needs an object, as far as I can see sFormName is a string.

I think if you wanted to do anything like this you might need to look into using the UserForms collection.

What is it you are trying to do anyway?

Are the 2 forms completely different?
 
Upvote 0
Yes, just as an experiment I created 2 or 3 differnet forms. They all contain the same info but in different formats, layouts, etc.

I wanted to have a simple interface where they could choose.

If the user chooses "UserForm2" from the combo box, then UserForm2 opens. If they choose "UserForm3" then UserForm3 opens, and so on.

Make sense? It's not practical from a long-term perspective but I need to know how to make this work for a demo.
 
Upvote 0
Why not just have separate subs for each userform?

Then you could use something like Application.Run to run the appropriate sub based on the selection in the combobox.

Or a Select Case structure, again based around what's selected from the combobox.

That wouldn't be too difficult to set up.:)
 
Upvote 0
Maybe something along this line - ohhh should have been private subs - but no biggie

Code:
Sub ComboBox1_Change()
Dim sFormName As String
sFormName = UserForm3.ComboBox1.Value
    Select Case sFormName
        Case "NameUhave1"
            Call LoadForm1
        Case "NameUhave2"
            LoadForm2
    End Select
    
End Sub
Public Sub LoadForm1()
    Load FormName1
    FormName1.Show
End Sub
Public Sub LoadForm2()
    Load FormName1
    FormName1.Show
End Sub
 
Upvote 0
Thanks Rasm. This worked well. Had a little trouble because the forms were set as modal but once I adjusted that it worked fine.
 
Upvote 0
Code:
Userforms.add(sformname).show
should also work.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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