Visual Basic - 变量名称自动递增/循环

时间:2012-11-10 22:55:37

标签: vba variables

我想要这样做是否有办法:

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

像这样做?

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

1 个答案:

答案 0 :(得分:6)

假设这些控件位于用户窗体内,请执行以下操作:

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