尝试将文本框值分配给字符串时出现“无效说明符”

时间:2014-01-20 07:19:06

标签: excel-vba textbox vba excel

我正在尝试将一个文本框(workbook1)值分配给字符串变量(在workbook2中)。我尝试了这段代码但是当我运行这段代码时,我得到一个运行时错误“无效的限定符”,而TextBox1在行Set Wbk1 = Workbooks.Open(TextBox1.Value)中突出显示

Dim Sh1 As Worksheet, Sh2 As Worksheet, Sh3 As Worksheet
Dim tmp1 As String, tmp2 As String, tmp3 As String
Dim TextBox1 As String
Dim TextBox1 As String
Dim TextBox1 As String
tmp1 = Sheets("Sheet1").TextBox1.Value
If tmp1 = "False" Then Exit Sub
tmp2 = Sheets("Sheet1").TextBox2.Value
If tmp2 = "False" Then Exit Sub
tmp3 = Sheets("Sheet1").TextBox3.Value
If tmp3 = "False" Then Exit Sub
Set Wbk1 = Workbooks.Open(TextBox1.Value)
Set Wbk2 = Workbooks.Open(TextBox2.Value)
Set Wbk3 = Workbooks.Open(TextBox3.Value)
Set Sh1 = Wbk1.Sheets("Sheet1")
Set Sh2 = Wbk2.Sheets("Sheet1")
Set Sh3 = Wbk3.Sheets("Sheet1") 

texboxes将拥有工作簿的路径,我想在tmp1,tmp2和tmp3中使用该工作簿,以便sh1将成为textbox1中所选工作簿的sheet1,依此类推..所以我怎样才能实现?谢谢

1 个答案:

答案 0 :(得分:1)

您需要为相关工作簿和工作表创建对象,然后使用它们。

例如(未经测试)

Sub Sample()
    Dim thisWb As Workbook, thatWb As Workbook
    Dim thatWs As Worksheet
    Dim tmp1

    Set thisWb = ThisWorkbook

    Set thatWb = Workbooks("Blah Blah.xlsx")
    Set thatWs = thatWb.Sheets("Sheet1")

    tmp1 = thatWs.TextBox1.Value
End Sub