macro for activate workbook with partial name

shahdelsol

Active Member
Joined
Jul 21, 2009
Messages
276
Office Version
  1. 365
Platform
  1. Windows
So I am using this code to activate a workbook with partial name and it works great. What I am trying to add is if workbook cannot be found I would get a msg box that says " file is not open"

Code:
Dim wb As Workbook

For Each wb In Application.Workbooks
   If wb.Name Like "partial name*" Then wb.Activate
Next wb

Here is what I added that didn't work:
Code:
If wb is nothing then 
 MsgBox "file is not open"
 
Last edited:

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try:
Code:
Dim Ct As Long
For Each wb In Application.Workbooks
    If wb.Name Like "partial name*" Then
        Ct = Ct + 1
        wb.Activate
        Exit For
    End If
Next wb
If Ct = 0 Then MsgBox "File not open"
 
Upvote 0
I would suggest that you put this function in a different module (I would suggest naming it 'Workbooks' or 'mWorkbooks') and make sure that the 'Option Compare' highlighted in red is at the top of your workbook (above all functions and subs). The reason for this is because the LIKE operator in VBA responds to the Option Compare Text by being case insensitive (conversely Option Compare Binary is the default and makes the LIKE operator case sensitive). Workbooks in the Excel.Application.Workbooks collection likewise are case insensitive and I'm pretty sure you don't want to have to try to guess workbook pattern names by case sensitivity.

Code:
[COLOR=#ff0000]Option Compare Text[/COLOR]

Public Function GetWorkbookByNamePattern(Pattern As String) As Workbook

Dim WB As Workbook
  
  For Each WB In Application.Workbooks
    If WB.Name Like Pattern Then
      Set GetWorkbookByNamePattern = WB
      Exit Function
    End If
  Next WB
  
  Set GetWorkbookByNamePattern = Nothing
  
End Function

Then back in your other code you can call the method like this:

Code:
Dim WB As Workbook

Set WB = GetWorkbookByNamePattern("partial name*")

If WB Is Nothing Then
  MsgBox "file is not open"
Else
  WB.Activate
End If
 
Upvote 0
Thank you Joe, This worked Great!

Try:
Code:
Dim Ct As Long
For Each wb In Application.Workbooks
    If wb.Name Like "partial name*" Then
        Ct = Ct + 1
        wb.Activate
        Exit For
    End If
Next wb
If Ct = 0 Then MsgBox "File not open"
 
Upvote 0
Great and Thank you for your help


I would suggest that you put this function in a different module (I would suggest naming it 'Workbooks' or 'mWorkbooks') and make sure that the 'Option Compare' highlighted in red is at the top of your workbook (above all functions and subs). The reason for this is because the LIKE operator in VBA responds to the Option Compare Text by being case insensitive (conversely Option Compare Binary is the default and makes the LIKE operator case sensitive). Workbooks in the Excel.Application.Workbooks collection likewise are case insensitive and I'm pretty sure you don't want to have to try to guess workbook pattern names by case sensitivity.

Code:
[COLOR=#ff0000]Option Compare Text[/COLOR]

Public Function GetWorkbookByNamePattern(Pattern As String) As Workbook

Dim WB As Workbook
  
  For Each WB In Application.Workbooks
    If WB.Name Like Pattern Then
      Set GetWorkbookByNamePattern = WB
      Exit Function
    End If
  Next WB
  
  Set GetWorkbookByNamePattern = Nothing
  
End Function

Then back in your other code you can call the method like this:

Code:
Dim WB As Workbook

Set WB = GetWorkbookByNamePattern("partial name*")

If WB Is Nothing Then
  MsgBox "file is not open"
Else
  WB.Activate
End If
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top