[VBA] How to Set Tab Order in UserForms to Improve Usability

目次

Introduction

Have you ever felt frustrated when pressing the Tab key on a VBA UserForm because the focus jumps around in a random, unintended order?

This sequence in which the cursor moves is called the “Tab Order.” When the tab order is set naturally to follow the flow of input fields, users can navigate smoothly using only the keyboard. This significantly improves the usability of the form.

In this article, I will explain two ways to correctly set the tab order for UserForms: the manual method and the VBA code method.

Method 1: Setting via the “Tab Order” Dialog (Manual)

The easiest and most intuitive way is to use the feature provided by the VBE (VBA Editor).

  1. Open the UserForm in Design View within the VBE.
  2. Click on a blank area of the form to select the form itself.
  3. From the menu bar, select View > Tab Order.
  4. The “Tab Order” dialog box will appear. Select a control name from the list and use the “Move Up” or “Move Down” buttons to arrange them in the desired order (from top to bottom).
  5. Click “OK” to finish.

In most cases, this manual method is the fastest and most reliable way to handle it.

Method 2: Setting TabIndex via VBA Code

If you need to change the tab order dynamically for some reason, you can set the .TabIndex property for each control using VBA code.

This code is typically written within the UserForm_Initialize event, which runs just before the form is displayed.

UserForm Code

' Event executed when the form is initialized
Private Sub UserForm_Initialize()
    
    ' Set the tab order starting from 0 using the TabIndex property
    ' 0 is the first focus
    Me.Name_TextBox.TabIndex = 0
    Me.Department_ComboBox.TabIndex = 1
    Me.Role_OptionButton1.TabIndex = 2
    Me.Role_OptionButton2.TabIndex = 3
    Me.Submit_Button.TabIndex = 4
    Me.Cancel_Button.TabIndex = 5
    
    ' Set focus to the first control
    Me.Name_TextBox.SetFocus
    
End Sub

Explanation of the Code

  • .TabIndex: This is a property of each control that specifies the tab order using an integer.
  • Starts from 0: TabIndex is a sequence starting from 0. The control with 0 receives focus first, followed by 1, and so on.
  • Sequential Numbers: While the numbers don’t strictly have to be sequential (e.g., 0, 5, 10), focus moves from the smallest number to the largest. However, managing it with a sequence starting from 0 is the easiest way to understand.
  • No Duplicates: If multiple controls have the same TabIndex, the focus movement might become unpredictable. Ensure that numbers do not overlap.

Just like with the manual setting, assigning TabIndex following the natural flow of user input (top to bottom, left to right) is key to creating a user-friendly form.

Summary

In this article, I explained how to set the “Tab Order” to improve UserForm usability.

  • Tab Order is the sequence in which focus moves when the user presses the Tab key.
  • For simple setups, using the “Tab Order” dialog in the VBE is recommended.
  • If dynamic settings are needed, set the .TabIndex property using VBA code, starting from 0.

Appropriate tab order is the foundation of a great user interface. A small amount of effort makes your form much easier to use, so please remember to set it up!

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

この記事を書いた人

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

目次