循环内的TextBox值

时间:2017-10-04 16:54:22

标签: vba loops textbox

我需要你的帮助,我有下一个代码的问题,我想在不同的单元格中写入用户输入的所有值,在Private Sub CommandButton1_Click()我只写了3个,但我可以'即使出现第一个值,也只会出现错误:

  

object不支持此属性或方法。

Dim Label1 As Object
Dim txtB1 As Control

For NL = 1 To NumeroLineas
    Set txtB1 = UserForm2.Controls.Add("Forms.TextBox.1", "TxtBx" & NL, True)

    With txtB1
        .Name = "TxtBx" & NL
        .Height = 25.5
        .Width = 150
        .Left = 150
        .Top = 18 * NL * 2
    End With    
Next NL
UserForm2.Show

'This is UserForm2
Private Sub CommandButton1_Click()
    Cells(10, 10) = Controls.TxtBx1.Value
    Cells(10, 11) = Controls.TxtBx2.Value
    Cells(10, 12) = Controls.TxtBx3.Value
End sub

1 个答案:

答案 0 :(得分:0)

您必须解决代码生成的控件,如下所示:Controls.Item("ControlName").Value

所以以下内容应该有效:

Private Sub CommandButton1_Click()
    Cells(10, 10) = Controls("TxtBx1").Value
    Cells(10, 11) = Controls("TxtBx2").Value
    Cells(10, 12) = Controls("TxtBx3").Value

    'This works too
    'Cells(10, 10) = Controls.Item("TxtBx1").Value
End sub

此外,Textbox.Name - 属性的设置是多余的 此行不是必需的:

.Name = "TxtBx" & NL