[英]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.