Referencing a Userform in another open workbook.

Engineer Joe

Well-known Member
Joined
Jun 10, 2005
Messages
549
i'm trying to show a userform from a different open workbook, but i can't get the syntax right...

i've assigned the other workbook name to a variable: helperfile

i was hoping i could just use something like:

helperfile.userformname.show

but this gives me an error.

Anyone know the correct method???
 

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.
As far as I know, you would need a function in the workbook with the userform that returns an Object set to an instance of the userform. You can then use the Show method on that. If you set a reference to the other workbook, you can call the function directly; if not, you need to use Application.Run.
HTH
 
Upvote 0
sorry VoGII. That doesn't work. It would if i were using a string to reference my other workbook but the variable is defined as an actual workbook object...

Working on understanding rorya's comment...
 
Last edited:
Upvote 0
Is a bit tricky. There are several ways. Here is one.

Create a workbook named Helper.xls. Add UserForm1. Enter this code into the workbook class.
Code:
Public Property Get HelperForm() As Object
    Set HelperForm = UserForm1
End Property
Save the workbook. Leave it open.

From any other open workbook, add this code in any public module.
Code:
Sub Example()
    Dim WBHelper As Workbook, UF As Object
 
    Set WBHelper = Application.Workbooks("Helper.xls")
    Set UF = WBHelper.HelperForm
    UF.Show
End Sub

If there is already an instance of userform1 loaded in Helper.xls, it is up to you to maintain a reference and provide that to clients by way of your property procedure. If not, a new instance of the form is created. In a real world project, you would not usually provide an implicit reference as such:
Set HelperForm = UserForm1

Instead, you would maintain your reference, rather initialized or not, at the module level of the workbook class in helper.xls.

Did I confuse you even more? :)
 
Last edited by a moderator:
Upvote 0
I hope I did not over-simplify the example. You have to be careful when opening a userform by an implicit reference. You should always provide yourself with a reference to the userform.

Implicit:
UserForm1.Show

Explicit:
Dim Uf as UserForm1
Set Uf = New UserForm1
Uf.Show

For example, the property procedure might check for an existing instance.
In Helper.xls...
Code:
Option Explicit

Private pHelperForm As UserForm1

Public Property Get HelperForm() As Object
    If Not pHelperForm Is Nothing Then
        Set HelperForm = UserForm1
    Else
        Set HelperForm = New UserForm1
    End If
End Property

Or perhap you might add an argument to request a new instance even if one already exists...

Code:
Private pHelperForm As UserForm1

Public Property Get HelperForm(Optional NewInstanceOnly As Boolean) As Object
    If Not pHelperForm Is Nothing And Not NewInstanceOnly Then
        Set HelperForm = UserForm1
    Else
        Set HelperForm = New UserForm1
    End If
End Property

I hope I'm being clear... :)
 
Upvote 0
Similar to Tom's. WbkA contains userform1 and a function like this:
Code:
Function GetForm() As Userform1
    Set GetForm = Userform1
End Function

WbkB can then use:
Code:
Sub blah()
   Dim frm as Object
   set frm = Application.Run("WbkA!GetForm")
   frm.Show
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,605
Messages
6,120,473
Members
448,967
Latest member
visheshkotha

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