将变量分配给Texbox控件名称

时间:2019-03-30 12:05:06

标签: excel vba

我正在制作一个带有很多文本框输入的程序,稍后再提交,我想将文本框名称分配为变量,并使用循环将文本框的名称从Texbox1递增到Textbox 20 < / p>

Dim Y As Integer
Dim X As String
Dim I As Integer
For I = 1 To 20
X = "TextBox" & Y
X.Value = ""
Next I

2 个答案:

答案 0 :(得分:2)

我认为您正在寻找类似的东西(我是UserForm的名称):

Dim x As String
Dim i As Integer
For i = 1 To 20
   x = Me.Controls("TextBox" & i).Value
Next i

答案 1 :(得分:1)

使用在线资源(https://www.ozgrid.com/VBA/control-loop.htm

您可以遍历它们而不必添加变量名:

Private Sub UserForm_Initialize()
Dim cCont As control
For Each cCont In Me.Controls
    cCont.Value = "foo"
Next cCont
End Sub