Subscript out of range error in VBA

UserPrabha

New Member
Joined
Jul 7, 2023
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
I am trying to run this code, it successfully copies the first two sheets and then throws up Error 9. This is my code.

VBA Code:
Sub Copy_Multiple_Sheets()

Dim Source As String
Dim Destination As String

Source = "TR B.xlsx"
Destination = "Load B.xlsx"

Dim Worksheets As Variant
ReDim Worksheets(12)

Worksheets(1) = "apr"
Worksheets(2) = "may"
Worksheets(3) = "june"
Worksheets(4) = "Jul"
Worksheets(5) = "Aug"
Worksheets(6) = "Sep"
Worksheets(7) = "oct"
Worksheets(8) = "nov"
Worksheets(9) = "dec"
Worksheets(10) = "jan"
Worksheets(11) = "feb"
Worksheets(12) = "mar"

Dim i As Variant
For i = 1 To UBound(Worksheets)
Workbooks(Source).Sheets(Worksheets(i)).Copy _
After:=Workbooks(Destination).Sheets(Workbooks(Destination).Sheets.count)
Next i

End Sub

This is where the error subscript out of range is pointing to . Can you please help

VBA Code:
Workbooks(Source).Sheets(Worksheets(i)).Copy _
After:=Workbooks(Destination).Sheets(Workbooks(Destination).Sheets.count)
 
Last edited by a moderator:

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Welcome to the MrExcel board!

When posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊

Just a guess, but in your code you have the worksheet name "june" but all the others are 3 letters. What is the actual name of that sheet in the source workbook?

Also, it is a bad idea to use variable names that are already words used by vba. You have used 'Source',' Destination' and 'Worksheets' as variable names and all of those words are already used by vba. Errors often stem from that.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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