FrenchCelt
Board Regular
- Joined
- May 22, 2018
- Messages
- 214
- Office Version
- 365
- Platform
- Windows
Hello,
I have some VBA code I use regularly when constructing macros that switch back and forth between different workbooks:
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?
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?