Search code examples
excelvbauserform

How to prevent Excel UserForm from shrinking/expanding autonomously?


My Excel UserForms contain a variety of objects, including text boxes, combo boxes, radio buttons, etc. The UserForm and the objects on the UserForm shrink and expand when my laptop is on a docking station and the VBA window is open on a larger monitor.

When I access the UserForm editor from the Forms tab in VBA, I can drag the UserForm resize handles and the objects in the UserForm will immediately snap back to their original state, but I want to do this programmatically so that the end user will not deal with shrunken/expanded UserForms.

I have tried resizing the UserForm upon opening (UserForm_Initialize), but it seems as if the shrinking/expanding takes place while the UserForm is not active, meaning that my UserForm resizing only acts to return the UserForm to its shrunken/expanded state and not its original state.

Sub UserForm_Initialize()

 Call ResizeUserform(Me)

End Sub

Sub ResizeUserform(UserForm_Name As Object)

 UserForm_Name.Width = UserForm_Name.Width + 0.001
 UserForm_Name.Width = UserForm_Name.Width - 0.001

 UserForm_Name.Height = UserForm_Name.Height + 0.001
 UserForm_Name.Height = UserForm_Name.Height - 0.001

End Sub

Solution

  • Don't leave your form's dimensions ambiguous or prone to logic circularity (i.e. as a function of itself); set them up before loading/showing.

    i.e.:

       'where XX and YY are integer constants:
       With YourFormName
            .width=XX
            .height=YY
            .show
        end with
    

    If you absolutely need to incur in circular statements, do it indirectly by storing your calculated variable in a global/local variable, and then proceed to declare its properties (i.e. YourFormName.width=variable)

    Good luck!.