VBA EXCEL: Fixing “Runtime-error '9'”: Subscript out of range

krodriguez

Board Regular
Joined
Jul 11, 2012
Messages
119
Hello,

I have the below code which attempts to select several sheets in a wordbook and save them on a separate file, but I'm getting the “Runtime-error '9'”: Subscript out of range, can someone assist to fix it? Thanks

Sub SaveSheetsAsFiles()
Dim SheetsToSave
SheetsToSave = Array("Rainbow", "RLN-Net Realization RLN-Red Rev RLN-COGS", "RLN-Logistics", "RLN-R&D", "RLN-Selling RLN-Administrative", "RLN-Advertising", "RLN-Sales Promo") 'change tab names to suit
Application.ScreenUpdating = False
For Each sht In Sheets(SheetsToSave)
sht.Copy
'file location is same as workbook the code is in. Change to suit
'file format is .xlsm - change to suit
ActiveWorkbook.SaveAs Filename:=sht.Name & ".xlsm", FileFormat:=52
Next sht
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hi,
that means that the array can not store that amount of values... and that is as you did not declare the array in the first place..

dim arrSheets() as variant

then you need to redim it so it can hold the array or you declare a fixed size array(0 to 6) in your case ..
 
Upvote 0
You need to declare a variable for the array

Sub SaveSheetAsFieles()
dim SheetToSave(0 to 6)as variant

if you only have 7 different sheet names or not more.. if you need more you would to change it to (0 to 10) holds 11 arrays ...

then it should work try it out...
 
Upvote 0
The error is telling you that one or more of the sheets don't exist.
For instance is this "RLN-Net Realization RLN-Red Rev RLN-COGS" 3 different sheets (its too long to be a single sheet name)?
If so it should be "RLN-Net Realization", "RLN-Red Rev", "RLN-COGS"
 
Upvote 0
@silentwolf
When assigning values to array like the op is doing you do not need to specify the size of the array.
There is nothing wrong with the code except the sheet names have not been properly entered.
 
Upvote 0
Hi Fluff,

ok thanks for the clarifying! Sorry that I made a wrong valueation of the problem! Ups :) But luckely you put him on the right track!! ;)
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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