Assign value to textboxes when UserForm loads using VBA

← Prev

Let us assume, I am using a UserForm in Excel for data entry. There are few textboxes and I want to populate the boxes with (defualt) values when the form loads. I can assign some initial (default) values to the textboxes using the UserForm_Initialize() event.
Private Sub UserForm_Initialize()
    txtName.Value = "Arun"
    txtJob.Value = "Programmer"
    txtPhone.Value = "123456789"
End Sub

The Initialize() event procedure is used to prepare a UserForm. You can declare variables, assign value to the variables and/or create and add dynamic controls on a UserForm with in the "Initialize()" event.

In the above code, I have three textboxes to which I have assigned few values during the initialization process. So, when the form loads, you will see values in the boxes. The values are hardcoded. However, you can assign "variables", add values to the variables and then populate the textboxes with values from variables, within the "Initialize()" event procedure.

Populate textbox with Cell value on form load

Now let us assume, I want to fill a textbox with "data extracted from a cell" when the UserForm loads.

I have hardcoded the name. But for the job, I have a master table in "Sheet2" and I'll get the value from "A3" cell and assign the value.

Private Sub UserForm_Initialize()
    txtName.Value = "Arun"
    txtJob.Value = Sheet2.Range("A3").Value
End Sub

➡️ More UserForm examples

← Previous