Activating Workbook Regardless of Capitalization

FrenchCelt

Board Regular
Joined
May 22, 2018
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have some VBA code I use regularly when constructing macros that switch back and forth between different workbooks:

VBA Code:
For Each w In Application.Workbooks
        If (w.Name) Like "Workbook Name" Then
            Exit For
        End If
    Next w
        If Not w Is Nothing Then
        w.Activate
        Else
            MsgBox "No workbook open!"
        End If

It's been very useful until now, as I have a workbook where sometimes the filename is capitalized and sometimes it's not. If there is a mismatch, the code errors out. I could simply use code to save the file with a specific name so that it always matches, but I like to first activate the correct workbook before saving to avoid someone accidentally running the code on the wrong workbook and saving that with the incorrect filename and completely blowing the process with the remaining code. Is there a way to do this so that the code will work even if the workbook is "Workbook Name" or "WORKBOOK NAME" or "workbook name" or any other capitalization variation?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hello,

I have some VBA code I use regularly when constructing macros that switch back and forth between different workbooks:

VBA Code:
For Each w In Application.Workbooks
        If (w.Name) Like "Workbook Name" Then
            Exit For
        End If
    Next w
        If Not w Is Nothing Then
        w.Activate
        Else
            MsgBox "No workbook open!"
        End If

It's been very useful until now, as I have a workbook where sometimes the filename is capitalized and sometimes it's not. If there is a mismatch, the code errors out. I could simply use code to save the file with a specific name so that it always matches, but I like to first activate the correct workbook before saving to avoid someone accidentally running the code on the wrong workbook and saving that with the incorrect filename and completely blowing the process with the remaining code. Is there a way to do this so that the code will work even if the workbook is "Workbook Name" or "WORKBOOK NAME" or "workbook name" or any other capitalization variation?
You could maybe modify the second line to
Code:
If ucase(w.Name) Like "WORKBOOK NAME" Then
cause UCASE to convert to upper case and then compare to an uppercased string
 
Upvote 0
Unfortunately that didn't work. I still get the same error message if the file name capitalization does not match:

1713871286059.png


On this line:

VBA Code:
If Not w Is Nothing Then

But it works fine if the capitalization matches.
 
Upvote 0
What happens if you replace this:
VBA Code:
        If (w.Name) Like "Workbook Name" Then

With this:
(where = 0 means a match, and vbTextCompare means its not case sensitive)

VBA Code:
        If StrComp(w.Name, "Workbook Name", vbTextCompare) = 0 Then
 
Upvote 0
I have logged out for the night but you are going to have to give us a specific example so we can test the same scenario.
 
Upvote 0
I would have to send the entire file. It's ok, I'll just engrain the notion that the people who use the macro have to run it while the correct workbook is active and that any screwups are their fault. 😆 I like to dummyproof my macros to encompass multiple possible points of failure due to incompetence on the part of the people I create them for (none of whom are Excel savvy in the least), but it's not always possible.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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