Search code examples
excelvbauserform

Excel VBA - Change userform height and width on click event


In Excel 365 a userform named "frmVorlage" is used.

A label named lblTtlMaximize is intended to change the size of the userform on click. This Userform starts with width = 455,25 and height = 1023,75 (set in the properties box)

Following code does only change the position, but not the height neither the width:

Private Sub lblTtlMaximize_Click()
        frmVorlage.Height = 1080
        frmVorlage.Width = 1920
        frmVorlage.Top = 0
        frmVorlage.Left = 0
End Sub
  • Made sure the sub is executed by putting in "Debug.Print frmVorlage.Height" => Shows 1080 but the actual height of the userform is not changed
  • If i put the same code in the initialize event it works and the Userform starts with width 1920 and height 1080
  • Me.Width and Me.Height does not work either

I am out of ideas how to fix this, since the .Top and .Left properties are changed on click but the .Height and .Width are not...


Solution

  • If you use the form correctly, that is to say you let the user work with a new instance of your frmVorlage form, you will want to set the instance properties using the Me keyword.

    Sub ShowMyForm()
        Dim uf As frmVorlage
        Set uf = New frmVorlage
        
        uf.Show
    End Sub
    

    Form Code

    Private Sub lblTtlMaximize_Click()
            Me.Height = 1080
            Me.Width = 1920
            Me.Top = 0
            Me.Left = 0
    End Sub
    

    If you set frmVorlage properties, you are actually just setting the form class's properties. You can see that if you show the form class (frmVorlage.Show), rather than an instance of the form, then your OnClick method works as you have it.