Preston
Preston

Reputation: 8187

Preserve the position of User Forms VBA

I'd like to preserve the position of a number of forms in the centre of the application, forms will be different sizes so below FormTop/FormLeft snippet can't be run again and again with the same effect. To do this I'm setting a public variable with the form's .top and .left values.

I get an error "Object Doesn't support this property or method", which seems odd as I was under the impression that the equation to make FormLeft would evaluate to a double data type. Both the lines work fine in the second section of code, what am I doing wrong?

Public FormTop As Double
Public FormLeft As Double

sub main()
    CentreForm UserForm2
end sub

Sub CentreForm(UForm As UserForm)
    With UForm
        If FormTop = 0 And FormLeft = 0 Then
'*********Errors appear on the below two lines******************
            FormLeft = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
            FormTop = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
            Debug.Print FormLeft, FormTop
        End If
        .StartUpPosition = 0
        .Left = FormLeft
        .Top = FormTop
    End With
End Sub

sub IWork()
With UserForm2
    .Left = Application.Left + (0.5 * Application.Width) - (0.5 * .Width)
    .Top = Application.Top + (0.5 * Application.Height) - (0.5 * .Height)
end with
end sub

Upvotes: 0

Views: 179

Answers (1)

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19077

If you can't solve your problem based on comment suggestion please change this line:

Sub CentreForm(UForm As UserForm)

into

Sub CentreForm(UForm As Object)

Upvotes: 1

Related Questions