Macro needs to ignore unique tab name

YLand

New Member
Joined
Oct 25, 2023
Messages
7
Office Version
  1. 2010
Platform
  1. Windows
I've created a Macro to format my monthly reports every month. The problem I am having is that my report automatically downloads every time with a different Tab Name. Today the tab is named 2023-10-24@01_22_report and tomorrow it will automatically be 2023-10-25@01_22_report. My macro does not recognize the report because of the different Tab name. What do I need to change on my macro? I've tried to google and follow other responses, but can't figure out where exactly I need to make the changes, I am not familiar with VBA and codes. Here is what my code looks like, please tell me what I need to change or add.

1698270225156.png

1698270249615.png
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
my report automatically downloads every time with a different Tab Name
Does the report you download every time only have one Tab? If so, you could refer to it by index number rather than actual name. So instead of:

VBA Code:
ActiveWorkbook.Worksheets("2023-10-24@01_22_report").etc

you could use:
VBA Code:
ActiveWorkbook.Worksheets(1).etc

Although you would be better off assigning a variable to that name, like this:
VBA Code:
Dim ws as Worksheet
Set ws  = ActiveWorkbook.Worksheets(1)

Then for the rest of the code you simply refer to ws
 
Upvote 0
The original report only has 1 tab (see pic), my macro creates 3 different tabs based on that data. Could I still use a variable? If so, where would I change it? Sorry, again, I am new to this and you might need to show me.
1698337610588.png


1698337732525.png
 

Attachments

  • 1698337647734.png
    1698337647734.png
    94 KB · Views: 1
Upvote 0
If you look in the project window (upper left in your image) you can see that the sheet has the code name of Sheet1.
Sheet1("2023-10-24@01_22_report")

If the sheet of interest is always Sheet1 in your project, then because the sheet is in the file that contains the VBA code, you can safely refer to the sheet by its code name rather than its Tab name. So in your code you can replace every line that looks like this:

VBA Code:
ActiveWorkbook.Worksheets("2023-10-24@01_22_report").<something>

with this:
VBA Code:
Sheet1.<something>

for example
VBA Code:
ActiveWorkbook.Worksheets("2023-10-24@01_22_report").Sort.SortFields.Clear

becomes
VBA Code:
Sheet1.Sort.SortFields.Clear

and that way, you don't have to change your code with every new report that's run.
 
Upvote 0
Awesome, thank you for the explanation. It works until I get to this section. What should I be changing this to? I changed it to Sheet1.
1698345819523.png
 
Upvote 0
Most "Select" lines are unnecessary. Delete that line - and the one before it (Range("B1").Select) which adds nothing to your code functionality.
 
Upvote 0
You have been very helpful, I did that and now some of the formatting is off. I will continue to play with it following your instructions, thank you.
 
Upvote 0
When it comes to formatting, you need to be careful. You can still get rid of some of the Select lines, for example this:
VBA Code:
Columns("C:D").Select
Selection.NumberFormat = "0.00"

Could be simply written like this:
VBA Code:
Columns("C:D").NumberFormat = "0.00"
 
Upvote 0

Forum statistics

Threads
1,215,124
Messages
6,123,190
Members
449,090
Latest member
bes000

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