Need Flexible Code - Users keep changing Sheet names...

hellfire45

Active Member
Joined
Jun 7, 2014
Messages
464
Hey guys. So I have an automation that processes a lot of manual files. It's admittedly a bad process.

On one file the user keeps submitting the data with one of the worksheet names with a space at the end. They either submit it as "Shipment" or "Shipment " My code as seen below does not handle this issue.

Code:
Set source_sheet = Workbooks(strFileName).Worksheets("Shipment")

I need coding that will process the sheet with or without the space. I was hoping I would use a wildcard & "*" but I don't think this works.

Also, I saw people suggesting to use
Code:
 .worksheets(1)
This workbook has multiple worksheets and I wouldn't put it past the user to re-order them. So this doesn't help.

Any suggestions? Talking to them is not an option. Thanks.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
If those are the only choices, I'd use a simple brute force approach:

Code:
On Error Resume Next
Set source_sheet = Workbooks(strFileName).Worksheets("Shipment")
if source_sheet is nothing then Set source_sheet = Workbooks(strFileName).Worksheets("Shipment ")
on error goto 0
 
Upvote 0
Thanks. So I already have an on ERROR Resume Next in the code because this automation is rifling through a folder and processing like 100 different files...

So when it can't find the initial Sheet name, its going to close and skip the workbook. Is there anyway to get the
Code:
[COLOR=#333333]Set source_sheet = Workbooks(strFileName).Worksheets("Shipment")[/COLOR]
line to not throw an error and do some kind of like an IFERROR( try other sheet name) type of operation?
 
Upvote 0
If you've already got an On Error Resume Next then you don't need the error handling I added (though it's really bad practice to just stick OERN at the start of your code!). All you need is:

Code:
Set source_sheet = Workbooks(strFileName).Worksheets("Shipment")
if source_sheet is nothing then Set source_sheet = Workbooks(strFileName).Worksheets("Shipment ")
 
Upvote 0
Another option, this looks for any sheet name containing "shipment"
Code:
   Dim Ws As Worksheet
   For Each Ws In Workbooks(strFileName).Worksheets
      If Ws.Name Like "*Shipment*" Then
         Set source_sheet = Ws
         Exit For
      End If
   Next Ws
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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