run time error 9

andysh

Board Regular
Joined
Nov 8, 2019
Messages
111
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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
You have declared wb as a workbook, but are trying to set it to a worksheet.
Change the declaration to WorkSheet
 
Upvote 0
Ok, what happens if you run this
Code:
Sub chk()
 Workbooks("2019_-_Couriersheet_-_copy.xlsx").Activate
End Sub
 
Upvote 0
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.
 
Upvote 0
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:
Upvote 0
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
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,213,506
Messages
6,114,024
Members
448,543
Latest member
MartinLarkin

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