Passing userform parameter

confusion123

Active Member
Joined
Jul 27, 2014
Messages
400
I want to pass a parameter to activate a userform but for some reason it does not work.

I have a userform called usrApples.

Code:
Sub Main

Call PopulateForm(usr:=usrApples)

End Sub

Sub PopulateForm(usr As UserForm)

usr.Show ' FAILS HERE

End Sub


Instead, I have to do this:

Code:
Sub Main

Call PopulateForm(usr:=1)

End Sub

Sub PopulateForm(usr As Integer)

If usr = 1 then usrApples.Show

End Sub

What is wrong?

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
What is usrApples?

If it's the name of a userform does this work for you?
Code:
Sub Main()

    Call PopulateForm(usr:="usrApples")

End Sub

Sub PopulateForm(usr As String)
Dim frm As Object

    Set frm = UserForms.Add(usr)

    frm.Show
    
End Sub
 
Upvote 0
What is usrApples?

If it's the name of a userform does this work for you?
Code:
Sub Main()

    Call PopulateForm(usr:="usrApples")

End Sub

Sub PopulateForm(usr As String)
Dim frm As Object

    Set frm = UserForms.Add(usr)

    frm.Show
    
End Sub

Yes usrApples is the name of my userform.

Thanks, your method worked.

But why did mine fail?

BTW, in your method, why does declaring usr As Userform, as opposed to plainly Object, not work?
 
Last edited:
Upvote 0
I'm not sure.

By the way, try changing Userform to Object in your original code.
 
Upvote 0

Forum statistics

Threads
1,225,844
Messages
6,187,356
Members
453,419
Latest member
Plane11

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