VBA - Activate Workbook with name like...

ChrisFoster

Active Member
Joined
Jun 21, 2019
Messages
251
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I expect this is fairly simple.

I have a large amount of code, but I'm stuck at this particular point.
The code will activate different workbooks at certain points but they are all have static filenames apart from one of them. For the one that isnt static the one constant is always "- All Clients". So for example;

April 2020 - All Clients
May 2020 - All Clients
June 2020 - All Clients

Is there a one line of code I can use to open the workbook that is like ...- All Clients?
It will be the only workbook open that has a name like this so it can't be confused anywhere.

Cheers

Chris
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
It doesn't seem to like the use of wildards in "Windows(...).Activate", so I am not sure how to do it using just one line.
But we can use something like this block to activate a file whose name ends in All Clients.xlsx":
VBA Code:
    Dim wb As Workbook
    
    For Each wb In Application.Workbooks
        If Right(wb.Name, 16) = "All Clients.xlsx" Then
            wb.Activate
            Exit For
        End If
    Next wb
 
Upvote 0
It doesn't seem to like the use of wildards in "Windows(...).Activate", so I am not sure how to do it using just one line.
But we can use something like this block to activate a file whose name ends in All Clients.xlsx":
VBA Code:
    Dim wb As Workbook
   
    For Each wb In Application.Workbooks
        If Right(wb.Name, 16) = "All Clients.xlsx" Then
            wb.Activate
            Exit For
        End If
    Next wb
Thank-you.
 
Upvote 0
You are welcome.
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,722
Members
449,465
Latest member
TAKLAM

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