I have multiple excel workbooks open. I want to activate a workbook which has "Final" in its name.
Example: I have three open workbooks called "Workbook1.xlsx", "worKbook2.xlsm" and "workbookFinal.xlsx" open at the same time.
My VBA code is in "Macro.xlsm". Using VBA I want to activate the workbook which has "Final" in it. FYI .. all workbooks are in different paths.
loop through Workbooks
collection until finding the right named workbook:
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 *
.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.