代码选择名称在文本框中列出的表

时间:2016-04-08 18:55:23

标签: excel-vba vba excel

我有一个带有一个文本框的用户表单。我的工作表中有几个表。我想要的是当我运行userform并在其中输入表格表时,在按下提交时,它应该找到其名称在文本框中输入的表格,然后选择该表格的单元格。到目前为止,我已经编写了这段代码,因为我是初学者。

Private Sub CommandButton2_Click()

'Unprotect the Worksheet
Sheets("Control Heads").Unprotect Password:="google"

Dim ws1 As Worksheet, tbl As ListObjects, row As ListRow

Set ws1 = Sheets("Control Heads")
Set tbl = ws1.ListObjects("Me.TextBox2.Value")
Set row = tbl.ListRows.Add

End Sub

请仔细检查代码并告诉我我在哪里做错了。

由于

1 个答案:

答案 0 :(得分:0)

Private Sub CommandButton2_Click()

'Unprotect the Worksheet
Sheets("Control Heads").Unprotect Password:="google"

Dim ws1 As Worksheet, tbl As ListObject, row As ListRow 
'tbl should be dim as ListObject, not ListObjects

Set ws1 = Sheets("Control Heads")
Set tbl = ws1.ListObjects(Me.TextBox2.Value) '<-- no quotation mark
Set row = tbl.ListRows.Add

End Sub