Run-time error 9

zerofeng

New Member
Joined
Apr 6, 2024
Messages
6
Office Version
  1. 2021
Platform
  1. Windows
Hi Experts here,

Please help.
I have 2 workbooks below and I would like to copy worksheets from workbook 1(Master) to workbook 2( Term).
I have pre-created the sheets JAN24 to Dec24 for workbook 2.(Term)

As the Master workbook sheet(by months) is actually manually created whenever come to the particular month. I got run time error 9.
Anyway to solve it?
Thanks



VBA Code:
Sub copydata()

Dim I As Long
Dim sMonth As String
Set Master = Workbooks("Master 2024.xlsm")
Set Report = Workbooks("Term 2024.xlsm")

For I = 1 To 12
sMonth = Format(DateSerial(Year(Date), I, 1), "mmm")
Master.Worksheets(sMonth & 24).range("a2:h450").copy
Report.Worksheets(sMonth & 24).range(a2:h450). PasteSpecial paste:=xlPasteValues
Next

End sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Like this...
VBA Code:
Sub copydata()

    Dim I As Long, j As Long
    Dim sMonth As String
    Dim Master As Workbook, Report As Workbook
    Set Master = Workbooks("Master 2024.xlsm")
    Set Report = Workbooks("Term 2024.xlsm")
    
    For I = 1 To 12
        sMonth = Format(DateSerial(Year(Date), I, 1), "mmm")
        For j = 1 To Master.Sheets.Count
            If Sheets(j).Name = sMonth & 24 Then
                Master.Worksheets(sMonth & 24).Range("a2:h450").Copy
                Report.Worksheets(sMonth & 24).Range("a2:h450").PasteSpecial Paste:=xlPasteValues
                Exit For
            End If
        Next
    Next
End Sub
 
Upvote 0
You can try:
Report.Worksheets(sMonth & 24).range("a2:h450"). PasteSpecial paste:=xlPasteValues

or:
Report.Worksheets(sMonth & 24).range("a2"). PasteSpecial paste:=xlPasteValues
 
Upvote 0
@Jason5710 that won't fix OP's issue. Error 9 (subscript out of range) occurs when trying to refer to a non-existent object or variable. In the case shown, the error occurs because the OP code is doing all 12 months (for I = 1 to 12) but in the master spreadsheet the months only exist up to the current month.
As the Master workbook sheet(by months) is actually manually created whenever come to the particular month. I got run time error 9.
 
Upvote 0

Forum statistics

Threads
1,223,867
Messages
6,175,077
Members
452,611
Latest member
bls2024

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