[VBA] Macro to Align UserForm Controls (Position and Size Settings)

目次

Introduction

When placing multiple text boxes or buttons on a VBA UserForm, aligning their positions and sizes manually using the mouse can be tedious and often results in slight misalignments.

By using VBA code, you can bulk-configure properties such as .Top (position from the top), .Left (position from the left), .Width (width), and .Height (height) for multiple controls using a loop. This allows you to efficiently create beautiful and user-friendly forms that are perfectly aligned at the pixel level.

In this article, I will explain how to create a practical macro that stores multiple controls in an array and arranges them in a single vertical column with equal spacing.


VBA Sample Code for Aligning Controls

It is most common to write this code within the UserForm_Initialize event, which executes when the UserForm is initialized (just before it is displayed).

In this example, three controls—Label_Name, TextBox_Name, and SubmitButton—are arranged vertically with equal spacing.

UserForm Code

' Event executed when the form is initialized
Private Sub UserForm_Initialize()

    '== Define variables and constants ==
    Dim i As Long
    Dim controlsToAlign As Variant
    Const ITEM_HEIGHT As Long = 24  ' Height of each control
    Const ITEM_WIDTH As Long = 180  ' Width of each control
    Const VERTICAL_GAP As Long = 10 ' Vertical gap between controls
    Const MARGIN_TOP As Long = 12   ' Initial margin from the top of the form
    Const MARGIN_LEFT As Long = 12  ' Margin from the left of the form

    '== Store controls to be aligned in an array ==
    ' Controls are placed from top to bottom in the order they appear in the array
    controlsToAlign = Array(Label_Name, TextBox_Name, SubmitButton)
    
    '== Set position and size for each control using a loop ==
    For i = LBound(controlsToAlign) To UBound(controlsToAlign)
        With controlsToAlign(i)
            ' Set height and width
            .Height = ITEM_HEIGHT
            .Width = ITEM_WIDTH
            
            ' Set position from the left
            .Left = MARGIN_LEFT
            
            ' Calculate and set position from the top
            ' (Height of previous control + Gap) * Index number + Top margin
            .Top = MARGIN_TOP + (ITEM_HEIGHT + VERTICAL_GAP) * i
        End With
    Next i

End Sub

Preparation Before Execution

  1. Create a UserForm.
  2. Place one Label, one TextBox, and one CommandButton on the form.
  3. Change the (Name) property of each to Label_Name, TextBox_Name, and SubmitButton respectively to match the code.
  4. Paste the UserForm_Initialize procedure above into the form’s code module.
  5. When you display the form, the controls will align automatically.

Code Explanation

Private Sub UserForm_Initialize()

This is a special event procedure that runs once right before the form is displayed. It is the ideal place to perform initial configurations for the form and its controls.

Const … As Long

Using Const, layout settings (height, width, gap, and margins) are grouped as constants. If you want to fine-tune the design later, you only need to change the values in this section, which improves maintainability.

controlsToAlign = Array(Label_Name, TextBox_Name, …)

The Array function is used to store the control objects to be aligned. The order in which they are stored in the array is the exact order they will appear from the top of the screen. To change the order of the controls, simply rearrange them within this array.

For i = LBound(controlsToAlign) To UBound(controlsToAlign)

A For loop is used to process each control stored in the controlsToAlign array one by one. i represents the array’s index number (starting from 0).

.Top = MARGIN_TOP + (ITEM_HEIGHT + VERTICAL_GAP) * i

This is the core logic calculating the vertical position (.Top) of the control.

  • When i = 0 (First control): .Top = 12 + (24 + 10) * 0, resulting in a position of 12 (the top margin).
  • When i = 1 (Second control): .Top = 12 + (24 + 10) * 1, resulting in a position of 46.

By using the index number i, the macro shifts the placement downward by the height of the control plus the gap for each iteration, achieving a perfectly spaced layout.


Summary

In this article, I explained how to programmatically align multiple controls on a UserForm using VBA.

  • Perform layout processing within the UserForm_Initialize event.
  • Store target controls in an Array and process them in bulk using a For loop.
  • Achieve precise layouts by calculating the .Top, .Left, .Width, and .Height properties with formulas.

Once you master this method, you can respond quickly and accurately even if the number of controls increases or layout changes occur frequently. Please use this to create clear and user-friendly forms.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次