run time error 9

andysh

New Member
Joined
Nov 8, 2019
Messages
34
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,569
Office Version
365
Platform
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
30,569
Office Version
365
Platform
Windows
Ok, what happens if you run this
Code:
Sub chk()
 Workbooks("2019_-_Couriersheet_-_copy.xlsx").Activate
End Sub
 

andysh

New Member
Joined
Nov 8, 2019
Messages
34
I get the same error when I run the suggested code
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,569
Office Version
365
Platform
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

New Member
Joined
Nov 8, 2019
Messages
34
Ran the code and got...

|2019 - Couriersheet -copy.xlsx| 31
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
30,569
Office Version
365
Platform
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

New Member
Joined
Nov 8, 2019
Messages
34
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
30,569
Office Version
365
Platform
Windows
You're welcome & thanks for the feedback
 

Forum statistics

Threads
1,078,525
Messages
5,340,974
Members
399,401
Latest member
poiter54

Some videos you may like

This Week's Hot Topics

  • Problem with Radio Button's format control
    I am creating an employee evaluation template (a sample is below) Column A is the category Column B, C D, E and F will be ratings (unacceptable...
  • Last Display on userform to a Listbox
    [CODE=vba] lstdisplay.ColumnCount = 15 lstdisplay.RowSource = "A1:O600000" [/CODE] So when i do this it Displays everything on the sheet i am...
  • Rename and move files to a new location
    Dear all, I have an excel file with the following information. The actual file name is at column A but i want to rename it using the following...
  • Help with True/False Formula
    Hello! Am stumped how to fix this formula, in which my result returns 'True', but it should return False. =IF(AG2=True...
  • Clear extra characters from a provided range of cells
    Dear All, I have following code which gives me desired output to remove extra characters from a provided range. But it takes too much time when...
  • Help with Current and highest streaks
    Hi there, I've just joined the forum and this is my first post. I've already spent quite a bit of time searching the net and this forum for a...
Top