Search code examples
excelvbauserform

Update multiple text boxes in an Excel userform with data from variables


I am looking for a way to update multiple text boxes in an Excel userform with data from a variable array. The text boxes are named as TextBox1, TextBox2 etc. And I want to update these text boxes in a DO LOOP rather than updating each text box line by line.

The variable array will contain n values for the n text boxes in the user form.

I am quite new to the user forms; Any help is appreciated.

Thanks.


Solution

  • Try and customize this to fit your needs:

    ' Declare variables
    Dim arrValues As Variant
    Dim counter As Integer
    
    ' Initialize array
    arrValues = Array("a", "b", "c")
    
    ' Loop through array items
    For counter = 0 To UBound(arrValues)
    
        ' Refer to textboxes names from controls collections and assign array values
        Me.Controls("TextBox" & counter + 1).Text = arrValues(counter)
    
    Next counter