Runtime Error '9' Subscript out of range error

tomsov

New Member
Joined
Mar 31, 2017
Messages
24
Hello, this is driving me crazy as I've checked numerous times that my file location and name is correct. This bit of code keeps throwing the error 'Runtime Error 9, subscript out of range' and I can't fathom the reason why - I've checked and double checked that the file names, location, extension is exactly right as to what and where it is on my PC but continue to get the error. The error is always on the the Set wsDest = part of the code. Help much appreciated!

Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long

Set wsCopy = ThisWorkbook.Worksheets("Sage Output")
Set wsDest = Workbooks("C:\Users\minee\Desktop\InvoiceRecordSheet.xlsx").Worksheets("SageBook")

... [code to copy etc]
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try this
VBA Code:
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
dim wbDest as Workbook

Set wsCopy = ThisWorkbook.Worksheets("Sage Output")
set wbDest = Application.Workbooks.Open("C:\Users\minee\Desktop\InvoiceRecordSheet.xlsx")
Set wsDest = wbDest.Worksheets("SageBook")
 
Upvote 0
The file has to be open before you can reference it like & then you just use the name of the workbook, not the full path.
 
Upvote 0
GWteB, again - absolute star!!!! Worked perfectly! You are a master at digging me out of holes!

Thank you, it is really appreciated.
 
Upvote 0
Sorry to bother you again, then to save and close the workbook I'm using this but getting the error: Error '13' Type Mismatch on the Application.Workbooks Close statement.

Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim wbDest As Workbook

Set wsCopy = ThisWorkbook.Worksheets("Sage Output")
Set wbDest = Application.Workbooks.Open("C:\Users\minee\Desktop\InvoiceRecordSheet.xlsx")
Set wsDest = wbDest.Worksheets("SageBook")

lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row

lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row

wsCopy.Range("A3:J" & lCopyLastRow).Copy _
wsDest.Range("A" & lDestLastRow)

Application.Workbooks(wbDest).Close SaveChanges:=True
 
Upvote 0
try
VBA Code:
wbDest.Close SaveChanges:=True
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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