简体   繁体   English

VBA:使用类似名称的选项卡打开多个工作簿:如何引用特定工作簿?

[英]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成为意外工作簿。

Referring to a Second Workbook参考第二个工作簿

  • If you only have two workbooks open, then you refer to the workbook containing this code with 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.

相关问题 打开多个工作簿时如何在特定工作簿中引用正确的工作表? - How to refer correct worksheet in specific workbook when multiple workbooks are open? 如何在VBA中引用打开的工作簿? - How to refer to an open workbook in VBA? 如何使用 VBA 引用另一个工作簿中路径和名称的单元格打开多个 Excel 工作簿 - How to Open Multiple Excel workbooks using VBA referencing cells in another workbook for the path and name VBA 从第一个工作簿打开两个工作簿,在第二个工作簿中打开 select 特定工作表 - VBA to open two workbooks from first workbook and select specific sheet in second workbook 如何从 PowerPoint VBA 参考 Open Excel 工作簿? (没有 GetObject) - How to refer to an Open Excel Workbook from PowerPoint VBA ? (without GetObject) Excel VBA:将多个工作簿合并为一个工作簿 - Excel VBA: Combine multiple workbooks into one workbook VBA:将多个工作簿(具有多个工作表)中的特定单元格复制到单个工作簿 - VBA : Copy Specific cells from Multiple Workbooks(having Multiple Worksheets) to a single WorkBook 在多个工作簿中定位一个特定的工作簿 - Target one specific workbook among multiple workbooks 使用VBA或宏将Sheet1从多个工作簿中的数据从特定文件夹导入单个工作簿 - Import Sheet1 Data From Multiple Workbooks From Specific Folder Into Single Workbook Using VBA Or Macros VBA 打开工作簿/检查工作簿是否打开最佳实践以及原因 - VBA Opening Workbooks/Checking if Workbook is Open Best Practice and Why
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM