load userform

codesmith

Active Member
Joined
Apr 23, 2008
Messages
257
hi all, why is this giving me a type mismatch error?


Code:
Sub loadMenu(menu As UserForm)
    Load menu
    menu.Show
    
    'usage: call loadmenu(mainMenu) ***Where mainMenu is a menu object
    
End Sub


thanks in advance...
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hello codesmith,
Not sure why you're getting a type mismatch, I just get a compile error.
Anyway, (for all I know about what you're doing) this seems like a bit more code than needed. From what I can tell, all you'd need would be:
Code:
Sub loadMenu()
    menu.Show
End Sub
in a standard module. (Assuming mainMenu is a userform, they can't show without loading first so you don't need to tell it to load beforehand.)

Hope it helps. (If it doesn't then perhaps you can elaborate a bit.)
 
Upvote 0
I agree - I'm not sure you need the variable passed here:

Sub LoadMenu()
Dim Menu as UserForm1
Set Menu = New UserForm1
Menu.Show
End Sub

Showing the form will load it if it isn't already loaded. Does this form lose scope when the sub ends? I've never loaded a form this way.
 
Upvote 0
you see i want to create a routine which accepts a menu name, and loads it (displays too).

similarly i want to create another which will close and hide it.
 
Upvote 0
Normally you Dim the userform object in the main/calling sub...You could instantiate the form right off the bat I guess...

Code:
Sub Main()
Dim menu as new UserForm1
~~code
Call LoadMenu(menu)
~~code
End Sub

I believe that instantiating the object in the declarations takes more memory, but memory is cheap these days. I'm not sure I see why not just:

Code:
Sub Main()
Dim menu as UserForm1
~~code
Set menu = New UserForm1
menu.show
~~code
End Sub
Or
Code:
Sub Main()
Dim menu as New UserForm1
~~code
menu.show
~~code
End Sub

But I guess there's a reason. :biggrin:
 
Upvote 0
This form take string input
Code:
Dim menuName as String
MenuName = "Userform1"

Userforms.Add(MenuName).Show

or
Code:
Dim menuName As String
Dim newUF As Object

menuName = "Userform1"
Set newUF = UserForms.Add(menuName)
newUF.TextBox1.Text = "Hello"
newUF.Show
 
Last edited:
Upvote 0
how about if I already have a set of forms ?

say I had a user form called "subMenuXYZ"

and I wanted to go:

Code:
call loadForm("subMenuXYZ")
'where loadform() loads, then shows.
 
Upvote 0
You could substitute "subMenuXYZ" for "Userform1" in the posted code.

On the other hand, one userform with a Multi-Page control might be easier to work with than several userforms.
 
Upvote 0

Forum statistics

Threads
1,214,521
Messages
6,120,018
Members
448,937
Latest member
BeerMan23

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