Replacing sheet names with numbers

gmittar

Board Regular
Joined
Sep 16, 2013
Messages
62
Hi All,

I'm trying to clean up a macro written by my predecessor, and I'm having a tough time figuring out why it's not working, as it should be pretty easy (despite my novice skillset).

Original Macro:
Sub filter_reset_condensed()


Sheets(Array("Bud_Guide_2019_acct_condensed", "Bud_Guide_2019_acct_details")).Select
Sheets("Bud_Guide_2019_acct_condensed").Activate
Range("A6").Select
ActiveCell.FormulaR1C1 = "Select Property"

Sheets("Bud_Guide_2019_acct_condensed").Select


End Sub

I want to replace it with:
Sub filter_reset_condensed()


Sheets(Array(4, 30)).Select
Sheets(4).Activate
Range("A6").Select
ActiveCell.FormulaR1C1 = "Select Property"

Sheets(4).Select


End Sub

I get the error "subscript out of range" with the array line highlighted. Hopefully this is an easy fix that I'm just not seeing.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Are the sheets named "4" & "30"?
Or are you referring to the sheet index number?
 
Upvote 0
In that case you probably don't have 30 sheets in the active workbook.
What does the message box say if you run this
Code:
Sub chk()
MsgBox Sheets.Count
End Sub
 
Upvote 0
There are 14 sheets. The sheet numbers that I'm using are the ones that I'm seeing in the VBE window. eg. "Sheet4(Bud_Guide... etc.)"
 
Upvote 0
Those are code names & you can use them like
Code:
Sub filter_reset_condensed()


Sheets(Array(Sheet4.name, Sheet30.name)).Select
Sheet4.Activate
Range("A6").Select
ActiveCell.FormulaR1C1 = "Select Property"

Sheet4.Select


End Sub
 
Upvote 0
You have the sheet index which is the number of the sheets position so sheets(1) is the first sheet in the workbook.
You have the sheet name, which is the name you see on the tab.
& finally you have the sheet codename which you can see in the VBA project window, but (by and large) you can only use the codename for the workbook containing the macro.
 
Upvote 0
Fluff,

Thanks for your help on this. One more question for a different macro.

I have a macro that compiles data from about 65 different excel files based on a specific range on a specific sheet in each file. I've been using the index, but someone inserted a sheet before the target sheet, which returned data from a different sheet that intended.

Since the macro is in my personal macro file, not the individual files, would it be best to refer to the sheets by their sheet name instead? In this case, the name on the tab is "Inputs". I hate to use sheet name since it can easily be changed and break the macro, but it sounds like it might be best in this scenario.

Thoughts?
 
Upvote 0
I would always recommend using sheet names over sheet index wherever possible.
You have just witnessed one of the reasons. Imagine if that was a master data sheet & rather than copying the info you were deleting it. Somebody changes the order of the sheets & you could lose all your data. At least with sheet names if somebody changes the name the macro will stop & all your data is intact.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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