Link to home
Start Free TrialLog in
Avatar of Roger
RogerFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Automated positioning of UserForm before UserForm.Show

I use an extension monitor, and my userForms sometime appear in odd places on the screens.
The following code places user form 'thisFrm' over the Application window:

    With thisFrm
        .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

I tried to automate the positions of several forms, and ran into problems below...

The line:     Debug.print "thisFrm.name = "; thisFrm.name         gave an Error. I find thisFrm IS Nothing
When the lines (below) followed by '**' were commented out, no errors occurred, and  MsgBox "WEEE" displayed (so the sub was run).

sub call1()
   Call userForm_Position("thisFrm")
end sub

Sub userForm_Position(formName As String)
    Dim frm As Object
    Dim thisFrm As Object
   
    For Each frm In VBA.UserForms
        If frm.name = formName Then  **
            Set thisFrm = frm  **
            exit sub  **
        End If  **
    Next
    Debug.print "thisFrm.name = "; thisFrm.name  **
    MsgBox "WEEE"
 
    With thisFrm  **
        .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  **

Is the enumeration   'Each frm In VBA.UserForms'   appropriate for forms that are not yet displayed ?
Ought the enumeration come from form objects specified in the VBE?

Floundering here!
Thanks
Kelvin
Avatar of Rgonzo1971
Rgonzo1971

HI,

it get to Wee because at

For Each frm In VBA.UserForms

it  probably doesn't find anything and continues after the next

Regards
SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Roger

ASKER

Rgonzo & Rory
Your comments address my problem.
I'll work on your solutions right now.

Many thanks!
Kelvin
Avatar of Roger

ASKER

Many thanks for immediate responses which made several points. I have both solutions up and running.
Best regards
Kelvin