Cenas
Cenas

Reputation: 45

Visual Basic - variable names auto increment / loop

Is there a way when I want to do this:

textbox1.text = x + i  
textbox2.text = x + i  
textbox3.text = x + i  

To do for example like this?

for l = 0 to 2   
  textbox(l) = x + i  
next  

Upvotes: 2

Views: 8398

Answers (1)

Doug Glancy
Doug Glancy

Reputation: 27478

Assuming these controls are inside a userform, do something like:

Private Sub FillTextboxes()
    Dim i As Long
    Dim x As Long

    x = 10
    For i = 0 To 2
        Me.Controls("textbox" & i + 1).Text = x + i
    Next i
End Sub

Upvotes: 6

Related Questions