简体   繁体   English

打开多个工作簿时如何在特定工作簿中引用正确的工作表?

[英]How to refer correct worksheet in specific workbook when multiple workbooks are open?

Sub debug_tester()

    Dim A As Workbook
    Set A = Workbooks.Open("D:\a.xlsm")

    Dim B As Workbook
    Set B = Workbooks.Open("D:\b.xlsm")

    A.Sheets("sheet1_in_test").range("A1").Value = "test" 'pop out "subscript out of range" on this line

End Sub

And "sheet1_in_test" does exist in A. If I change it to number (ie sheet(1)) B is the one changed. 而且“ sheet1_in_test”确实存在于A中。如果我将其更改为数字(即sheet(1)),则B是已更改的。

Edit: Correct the typos. 编辑:更正错别字。 Workbook A has one sheet named "sheet1_in_test". 工作簿A有一个名为“ sheet1_in_test”的工作表。 Workbook B has one sheet named "sheet1". 工作簿B有一个名为“ sheet1”的工作表。

Edit: Thanks sancho.s ! 编辑:谢谢sancho.s It seems like Workbook.Open cannot refer the workbook when the sub located on it. 看起来Workbook.Open子项位于它时无法引用该工作簿。 Using Set A = ThisWorkbook seems work, too. 使用Set A = ThisWorkbook似乎也可以。 I'm wondering why is that. 我想知道为什么会这样。

This sub , located in a separate workbook, performs as intended sub位于单独的工作簿中,按预期执行

Sub open_test()
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Set wb1 = Workbooks.Open("C:\Users\user1\Documents\a.xlsx")
    Set wb2 = Workbooks.Open("C:\Users\user1\Documents\b.xlsx")    
    wb1.Sheets("Hoja1").Range("A1").Value = "test"
    wb2.Sheets(1).Range("A1").Value = "test2"
End Sub

Try reproducing this. 尝试重现此内容。

Using Workbooks.Open() makes the opened workbook the active workbook. 使用Workbooks.Open()使打开的工作簿成为活动工作簿。 That's why using sheet(1) goes into workbook B. It's the last workbook opened and therefore the active workbook. 这就是为什么在工作簿B中使用sheet(1)的原因。这是最后打开的工作簿,因此也是活动的工作簿。 Use Workbook.Activate to make A the active workbook. 使用Workbook.Activate使A成为活动工作簿。

Sub debug_tester()

    Dim A As Workbook
    Set A = Workbooks.Open("D:\a.xlsm")

    Dim B As Workbook
    Set B = Workbooks.Open("D:\b.xlsm")

    A.Activate
    A.Sheets("sheet1_in_test").range("A1").Value = "test" 

End Sub

If you still get the "subscript out of range" message, the "sheet1_in_test" worksheet is not in the A workbook. 如果仍然收到“下标超出范围”消息,则“ sheet1_in_test”工作表不在A工作簿中。 Check the spelling between the code and the worksheet name. 检查代码和工作表名称之间的拼写。 This assumes that you are running the code from a 3rd workbook. 假定您正在从第3个工作簿中运行代码。

If you are running this code from workbook A, Excel will close the open instance of A and reopen it but the code won't be running. 如果您正在工作簿A中运行此代码,则Excel将关闭A的打开实例并重新打开它,但该代码将无法运行。 In that case you can use 在这种情况下,您可以使用

Set A = ThisWorkbook

Or if the code is in workbook B use 或者如果代码在工作簿B中使用

Set B = ThisWorkbook

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VBA:使用类似名称的选项卡打开多个工作簿:如何引用特定工作簿? - VBA: multiple workbooks open with similarly named tabs: how to refer to specific workbook? 如何引用工作簿中的工作表? - how to refer to a worksheet in a workbook? 使用 Powershell 将多个工作簿合并为一个工作簿工作表 - Combining Multiple Workbooks Into One Workbook Worksheet With Powershell 选择具有多个工作簿的工作表打开狂 - selecting worksheet with multiple workbooks open crahses 在多个工作簿中定位一个特定的工作簿 - Target one specific workbook among multiple workbooks 如何在VBA中引用打开的工作簿? - How to refer to an open workbook in VBA? VBA:如何在将工作表作为函数的参数时解决正确的工作簿? - VBA: How to address the correct workbook when giving a worksheet as an argument to a function? 如何将打开的工作簿添加到“Application.Workbooks”集合和/或与工作簿交互 - How to add Open Workbook to “Application.Workbooks” collection and/or interact with Workbook 将数据从多个工作簿复制到一个新的工作表中,其工作表名称与工作簿名称相同 - Copying data from multiple workbooks into a new worksheet with the worksheet name same as the workbook name 如何使用 VBA 引用另一个工作簿中路径和名称的单元格打开多个 Excel 工作簿 - How to Open Multiple Excel workbooks using VBA referencing cells in another workbook for the path and name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM