iterate thru specific controls in a form using a loop

lets say i have 10 textboxes in a form named txt1 to txt10 but i would like to iterate only the txt3,txt6,txt8,txt9 and multiple their values to 2 i can type it like below but how can i do it using a loop

txt3.value = txt3.value * 2
txt6.value = txt6.value * 2
txt8.value = txt8.value * 2
txt9.value = txt9.value * 2

>Solution :

First define a array and store textbox numbers you want to iterate. Then traverse that array using for loop and execute operation you want. Try below sub.

Private Sub CommandButton1_Click()
Dim arr
Dim i As Long

    arr = Array(3, 6, 8, 9)
    For i = LBound(arr) To UBound(arr)
        Me.Controls("TextBox" & arr(i)) = Me.Controls("TextBox" & arr(i)) * 2
    Next i

End Sub

Leave a Reply