run time error 9

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
Hi

I have the below code to copy data from one workbook to another but keep getting a subscript out of range error on the line...

Set wb = Workbooks("2019_-_Couriersheet_-_copy.xlsx").Worksheets("Triage2")

Both workbooks are open and I've checked the file name is correct etc. What could the issue be?

Sub Export_data()

Dim ws As Worksheet, wb As Workbook

Set ws = Sheets("Triage")
Set wb = Workbooks("2019_-_Couriersheet_-_copy.xlsx").Worksheets("Triage2")
Dim lr As Long, lrw As Long, lc As Long

Application.ScreenUpdating = False

lrw = ws.Range("A" &Rows.Count).End(xlUp).Row

If lrw <> 1 Then

lc= ws.Cells(1, Columns.Count).End(xlToLeft).Column
lr= wb.Range("A" & Rows.Count).End(xlUp).Row + 1
ws.Range(ws.Cells(2, 1), ws.Cells(lrw, lc)).Copy
wb.Range("A" & lr).PasteSpecial xlPasteValues
ws.Range(ws.Cells(2, 1), ws.Cells(lrw, lc)).EntireRow.Delete

End If


Application.CutCopyMode = False

Application.ScreenUpdating = True

MsgBox" Exported ! "


End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,790
Office Version
  1. 365
Platform
  1. Windows
You have declared wb as a workbook, but are trying to set it to a worksheet.
Change the declaration to WorkSheet
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,790
Office Version
  1. 365
Platform
  1. Windows
Ok, what happens if you run this
Code:
Sub chk()
 Workbooks("2019_-_Couriersheet_-_copy.xlsx").Activate
End Sub
 

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105

ADVERTISEMENT

I get the same error when I run the suggested code
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,790
Office Version
  1. 365
Platform
  1. Windows
In that case that either that workbook isn't open, or it is spelt wrongly.
activate that workbook & run this
Code:
Sub chk()
 Debug.Print "|" & ActiveWorkbook.Name & "|", Len(ActiveWorkbook.Name)
End Sub
Then in the VB Editor look in the immediate window (normally below the main code window, Ctrl G will open it if needed) and copy what is printed there & post it here.
 

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105

ADVERTISEMENT

Ran the code and got...

|2019 - Couriersheet -copy.xlsx| 31
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,790
Office Version
  1. 365
Platform
  1. Windows
Did you copy/paste that, or retype it?
The reason I ask is there are only 30 characters in the file name, but it's reporting 31
 
Last edited:

andysh

Board Regular
Joined
Nov 8, 2019
Messages
105
Copied the file name displayed and it's accepting it.

Just need to get the code right for the cut/paste now...

Thanks for your help Fluff
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,790
Office Version
  1. 365
Platform
  1. Windows
You're welcome & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,130,042
Messages
5,639,730
Members
417,108
Latest member
Thein Than

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
Top