Userform shows up on the wrong monitor

smkyle

Board Regular
Joined
Sep 13, 2012
Messages
185
I have two userforms that I use. Recently, they have started showing up on the left monitor even though Excel is open on the right monitor. Any ideas?
 
Mike,

Here's two ways to approach this. Neither one is ideal.

You can pass the Userform as an Object type argument. This will allow you to Show the passed Userform, but it throw a Type Mismatch error when control is passed to the calling procedure.
Code:
'---in Project.xlsm
Sub Test()
    Call LaunchIT(UserForm1) '--throws error after call.
End Sub

'---In the .xlam file
Public Sub LaunchIT(frm As Object)
    With frm
        .StartUpPosition = 0
        .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
        .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
        .Show
    End With
End Sub

One option would be to use an error handler to supress that error.
I hesitate to recommend this though since I don't completely understand the reason for the error and there could be some other effect.
Code:
'---in Project.xlsm
Sub Test()
    On Error Resume Next
    Call LaunchIT(UserForm1)
    On Error GoTo 0
End Sub

This thread explains a cleaner, but more complex approach
http://www.mrexcel.com/forum/excel-questions/326834-referencing-userform-another-open-workbook.html

Here's an example that applies that approach...
Code:
'---in Project.xlsm
Sub Test1()
    Call LaunchIT(ThisWorkbook.Name, "GetForm1")
End Sub
Sub Test2()
    Call LaunchIT(ThisWorkbook.Name, "GetForm2")
End Sub

Function GetForm1() As UserForm1
    Set GetForm1 = UserForm1
End Function

Function GetForm2() As UserForm2
    Set GetForm2 = UserForm2
End Function

'---In the .xlam file
Public Sub LaunchIT(sWBName As String, sFunctionName As String)
    Dim frm As Object
    Set frm = Application.Run(sWBName & "!" & sFunctionName)
    With frm
        .StartUpPosition = 0
        .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
        .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
        .Show
    End With
End Sub

This makes me question whether placing LaunchIT in an Add-In where it is separated from the Userforms is good idea. Typically a primary benefit of putting code in an Add-In is that you can call it from any workbook. In the two examples above, special setup is required in the calling workbook. Rather than doing that special setup, it would seem simpler to just place the LaunchIT code shown in Post #7 of this thread in the calling workbook.
 
Last edited:
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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