简体   繁体   English

Excel VBA:如何激活没有全名的工作簿?

[英]Excel VBA: How to activate a workbook without its complete name?

I have multiple excel workbooks open.我打开了多个 Excel 工作簿。 I want to activate a workbook which has "Final" in its name.我想激活一个名称中包含“Final”的工作簿。

Example: I have three open workbooks called "Workbook1.xlsx", "worKbook2.xlsm" and "workbookFinal.xlsx" open at the same time.示例:我同时打开了三个名为“Workbook1.xlsx”、“worKbook2.xlsm”和“workbookFinal.xlsx”的工作簿。

My VBA code is in "Macro.xlsm".我的 VBA 代码在“Macro.xlsm”中。 Using VBA I want to activate the workbook which has "Final" in it.使用 VBA 我想激活其中包含“Final”的工作簿。 FYI .. all workbooks are in different paths.仅供参考..所有工作簿都在不同的路径中。

loop through Workbooks collection until finding the right named workbook:循环遍历Workbooks集合,直到找到正确命名的工作簿:

Sub wbs()
    Dim wb As Workbook

    For Each wb In Workbooks
        If InStr(wb.Name, "Final") > 0 Then
            wb.Activate
            Exit For
        End If
    Next
End Sub

Try the code below, using the Like Operator with the wild-card * .试试下面的代码,使用带有通配符*Like运算符。

Option Explicit

Sub FindFinalWorkbook()

Dim wb As Workbook

' loop through all open workbooks
For Each wb In Application.Workbooks
    If wb.Name Like "*Final*" Then '< -- check if workbook name is Like *Final*
        wb.Activate
        Exit For
    End If
Next wb

End Sub

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

相关问题 如何在不使用名称或至少使用名称的情况下激活excel工作簿? 提取的工作表具有动态名称 - how to activate an excel workbook without using its name or at least part of it? the extracted sheet has a dynamic name 如何通过另一个工作簿中的VBA对象名称引用Excel工作表? - How to refer to a Excel Worksheet by its VBA Object Name in another Workbook? Excel VBA打开工作簿及其部分名称 - Excel VBA open workbook with part of its name 如何激活使用VBA中的工作簿名称打开的工作簿 - How to activate a workbook that is open using the name of the workbook in VBA 如何在非VBA Excel上激活特定的工作簿退出 - How to Activate a specific Workbook on non-VBA Excel Quit Excel VBA:如何在不运行宏的情况下打开Excel文件,但希望在激活工作簿时运行宏? - Excel VBA: How do I open an Excel file without running macro but want macros to run when I activate workbook? 如何在VBA中自动命名新的Excel工作簿 - How to name a new Excel workbook automatically in VBA 添加“。” 到工作簿的名称而不更改其在 VBA 中的格式 - Adding a "." to the name of a Workbook without changing its format in VBA VBA,激活工作簿而不明确命名 - VBA, activate workbook without explicitly naming it 在不知道其名称的情况下从VBA激活Excel工作表? - Activate an excel sheet from VBA without knowing it's name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM