[英]VBA: multiple workbooks open with similarly named tabs: how to refer to specific workbook?
Say I have two workbooks open with two tabs both named "Tab1".假设我打开了两个工作簿,其中两个选项卡都名为“Tab1”。 If in VBA, I want to refer to something on a tab, one would say "Tab1," followed by what one is referring to.
如果在 VBA 中,我想引用选项卡上的某些内容,可以说“Tab1”,然后是指的内容。 eg, "Tab1.R1C1", etc.
例如,“Tab1.R1C1”等。
However, how does the code know what Tab1 I am referring to in this case?但是,在这种情况下,代码如何知道我指的是什么 Tab1?
If it's worth mentioning, the code I am running is inside one of the workbooks.如果值得一提,我正在运行的代码位于其中一个工作簿中。 So will it always default back to that workbooks tab 1?
那么它会始终默认返回到该工作簿选项卡 1 吗? If not, what does it do, and if yes, how would I make it refer to the tab 1 of the other workbook?
如果不是,它会做什么,如果是,我将如何让它引用另一个工作簿的选项卡 1?
For context, I need to do this in the case of calling the method "ChangePivotCache" and give it a SourceData.对于上下文,我需要在调用“ChangePivotCache”方法并为其提供 SourceData 的情况下执行此操作。
Without the code it's difficult to say definitively, but lets assume that you're using a range
object.没有代码很难明确地说出来,但假设您使用的是 object
range
。 When you put "Tab1!R1C1"
into that range object it uses the workbook of it's parent to make the determination.当您将
"Tab1!R1C1"
放入该范围 object 时,它会使用其父级的工作簿来做出决定。 Most (if not all) methods that take a range argument like that will require that some parent of theirs is a workbook or worksheet.大多数(如果不是全部)采用这样的范围参数的方法将要求它们的某些父级是工作簿或工作表。
In general if you're working with multiple workbooks best practice is to assign an object to them as you open them.通常,如果您使用多个工作簿,最佳做法是在打开它们时为其分配 object。 That object can then be used to anchor any range calls you make.
然后可以使用 object 来锚定您进行的任何范围调用。 Either by calling directly from it or making children from it.
要么直接从它调用,要么从它生成孩子。
For example:例如:
set workbook_code_is_in = Thisworkbook
set another_workbook = Application.Workbooks.Open(...)
set active_workbook = Activeworkbook
The first line will set workbook_code_is_in
to the workbook that the code is located in.第一行将
workbook_code_is_in
设置为代码所在的工作簿。
The second line will set another_workbook
to whatever you open.第二行会将
another_workbook
设置为您打开的任何内容。
The third line will set active_workbook
to whatever workbook currently has focus.第三行将
active_workbook
设置为当前具有焦点的任何工作簿。 This is dangerous and prone to errors.这是危险的并且容易出错。 It's very possible for a user to click a different workbook while a script is running and make the Activeworkbook an unintended one.
用户很可能在脚本运行时单击不同的工作簿并使Activeworkbook成为意外工作簿。
ThisWorkbook
and you can refer to the other workbook by using a For Each...Next
loop to compare the workbooks' names.ThisWorkbook
引用包含此代码的工作簿,并且您可以通过使用For Each...Next
循环来比较工作簿的名称来引用另一个工作簿。Option Explicit
Sub ReferToSecondWorkbook()
Dim wb1 As Workbook: Set wb1 = ThisWorkbook ' workbook containing this code
If Workbooks.Count <> 2 Then
MsgBox "You need to have only two workbooks open.", vbCritical
Exit Sub
End If
Dim wb2 As Workbook
For Each wb2 In Workbooks
If StrComp(wb2.Name, wb1.Name, vbTextCompare) <> 0 Then ' different
Exit For
' Else ' it is 'wb1'
End If
Next wb2
Dim ws1 As Worksheet: Set ws1 = wb1.Worksheets("Tab1")
Dim ws2 As Worksheet: Set ws2 = wb2.Worksheets("Tab1")
Debug.Print "First: ", ws1.Name, wb1.Name
Debug.Print "Second: ", ws2.Name, wb2.Name
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.