Passing Userform as Argument of Sub

animas

Active Member
Joined
Sep 28, 2009
Messages
396
In module I have:
Code:
Public Sub LoadUserForm(ByVal iform As UserForm)
    '//set form position
    With iform
        .Top = Application.Top + 170
        .Left = Application.Left + 30
    End With
    
    iform.Show vbModeless
End Sub

in worksheet module I have:
Private Sub Worksheet_Activate()
LoadUserForm (frmMenubar)
End Sub

Note that frmMenubar is the userform that i already created.

Error shows Type Mismatch. How to do this?
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
try passing the form name as a string then use:

With forms(iform)
Code:
        .Top = Application.Top + 170
        .Left = Application.Left + 30
    End With
 
Upvote 0
You have two issues - use:
Code:
Public Sub LoadUserForm(ByVal iform As Object)

and call it without the parentheses:
Code:
LoadUserForm frmMenubar
 
Upvote 0
Thanks
I tried rorya's code. It's working.

One issue i can't resolve.

Code:
    With iform
        .Top = Application.Top + 170
        .Left = Application.Left + 30
    End With
Above code was working when i put it in UserForm_Activate event sub, inside userform module code.

But now it has no effect.
 
Upvote 0
You need to override the startupposition:
Code:
Public Sub LoadUserForm(ByVal iform As Object)
    '//set form position
    With iform
        .startupposition = 0
        .Top = Application.Top + 170
        .Left = Application.Left + 30
    End With
    
    iform.Show vbModeless
End Sub
 
Upvote 0
Thank you. Forms are positioned in right place now. I put LoadUserForm frmMenubar inside worksheet_activate event of mySHEET module. I used Unload frmMenubar inside worksheet_deactivate event. Problem is When I activated mySHEET and selected another workbook frmMenubar still remains loaded. I needed it to be shown on mySHEET only. Nowhere else.

Where should I change?
 
Upvote 0
You could use the workbook_active and workbook_deactivate events too. On the workbook activate, check which sheet is the active one and then load your userform if the particular sheet is active.
 
Upvote 0
Users may copy my worksheet multiple times and add new sheets too. The above procedure will force me to hardcode the sheets name each time user adds sheet. I wanted to keep the code dynamic. Any workaround to this?
 
Upvote 0
Depends - how do you determine which sheets need the userform?
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

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