Subscript Out of Range - Copying from workbook to workbook

emyyra

New Member
Joined
Oct 5, 2016
Messages
4
I'm pretty new to VBA, so I'm not too familiar with the errors and exact coding rules. I'm getting a Run-time error '9': Subscript Out of Range when I use this macro:

Sub CopyStuff()


Range("d4:I15").Copy
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsm").Worksheets("Myyra").Range("d4:I15").Paste


End Sub


'Any suggestions would be appreciated. Thanks!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try:

Code:
Sub CopyStuff()


Range("d4:I15").Copy
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsm").Worksheets("emyyra").Range("d4:I15").Paste
End Sub
 
Upvote 0
Try:

Code:
Sub CopyStuff()


Range("d4:I15").Copy
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsm").Worksheets("emyyra").Range("d4:I15").Paste
End Sub

The sheet in the "Random Thing" workbook that I'm targeting is called "myyra", so "emyyra" won't work since it isn't a sheet in that workbook. I'm trying to have a range of cells copied into a specific workbook.
 
Upvote 0
Is the workbook named 'Random Thing.xlsm' open?

If it is you only need the workbook name to refer to it, you don't need the path.
 
Upvote 0
I'm pretty new to VBA, so I'm not too familiar with the errors and exact coding rules. I'm getting a Run-time error '9': Subscript Out of Range when I use this macro:

Sub CopyStuff()


Range("d4:I15").Copy
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsm").Worksheets("Myyra").Range("d4:I15").Paste


End Sub


'Any suggestions would be appreciated. Thanks!

I feel as though that Subscript Out of Range error is occurring because of where and how you are trying to call CopyStuff().

I believe the issue is with the following line of code:

Code:
Range("d4:I15").Copy

I'm guessing Excel doesn't know which range you are trying to reference. The next line of code shouldn't be returning any out of range errors because you've defined a hard path to the file, worksheet and range:

Code:
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsm").Worksheets("Myyra").Range("d4:I15").Paste

Could you please advise which workbook, worksheet, and where within your code you make this call to CopyStuff().

Edit: Try defining the workbook and worksheet in the first line of code.
 
Last edited:
Upvote 0
I feel as though that Subscript Out of Range error is occurring because of where and how you are trying to call CopyStuff().

I believe the issue is with the following line of code:

Code:
Range("d4:I15").Copy

I'm guessing Excel doesn't know which range you are trying to reference. The next line of code shouldn't be returning any out of range errors because you've defined a hard path to the file, worksheet and range:

Code:
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsm").Worksheets("Myyra").Range("d4:I15").Paste

Could you please advise which workbook, worksheet, and where within your code you make this call to CopyStuff().

Edit: Try defining the workbook and worksheet in the first line of code.

I tried it with the actual pathway, but then the error was on the first line of code:

Sub CopyStuff()


Workbooks("C:\Users\emyyra\Desktop\First Thing.xlsm").Sheets("Annual Hours").Range("d4:i15").Copy
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsx").Sheets("Myyra").Range("d4:i15").Paste


End Sub

The error occurs during the 2nd line of code in my first statement.
 
Upvote 0
try changing this line,

Code:
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsx").Sheets("Myyra").Range("d4:i15").Paste

to

Code:
Workbooks("C:\Users\emyyra\Desktop\Random Thing.xlsx").Sheets("Myyra").Range("d4:i15").PasteSpecial xlPasteAll
 
Upvote 0
You do not use/need the path when referring to an open workbook.

Try this.
Code:
Sub CopyStuff()

Workbooks("First Thing.xlsm").Sheets("Annual Hours").Range("d4:i15").Copy

Workbooks("Random Thing.xlsm").Worksheets("Myyra").Range("d4:I15").Paste


End Sub
Note - the workbooks 'First Thing.xlsm' and 'Random Thing.xlsm' have to be open.
 
Upvote 0
Not tested but does this help:

Rich (BB code):
Sub CopyStuff()
    Dim wbDestination As Workbook
    Dim CopyRange As Range
    Dim FolderPath As String, FileName As String
    
     Application.ScreenUpdating = False
     
    FolderPath = "C:\Users\emyyra\Desktop\"
    FileName = "Random Thing.xlsm"
    
    Set CopyRange = ThisWorkbook.Worksheets("Sheet1").Range("d4:I15")
   
    On Error Resume Next
    Set wbDestination = Workbooks(FileName)
    If wbDestination Is Nothing Then Set wbDestination = Workbooks.Open(FolderPath & FileName, ReadOnly:=False): Err.Clear
    
    On Error GoTo myerror
    CopyRange.Copy wbDestination.Worksheets("Myyra").Range("d4")
    
'close & save
    'wbDestination.Close True
      
myerror:
    Application.ScreenUpdating = True
    If Err > 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Change the copy sheet name shown in RED as required.

Dave
 
Last edited:
Upvote 0
Not tested but does this help:

Rich (BB code):
Sub CopyStuff()
    Dim wbDestination As Workbook
    Dim CopyRange As Range
    Dim FolderPath As String, FileName As String
    
     Application.ScreenUpdating = False
     
    FolderPath = "C:\Users\emyyra\Desktop\"
    FileName = "Random Thing.xlsm"
    
    Set CopyRange = ThisWorkbook.Worksheets("Sheet1").Range("d4:I15")
   
    On Error Resume Next
    Set wbDestination = Workbooks(FileName)
    If wbDestination Is Nothing Then Set wbDestination = Workbooks.Open(FolderPath & FileName, ReadOnly:=False): Err.Clear
    
    On Error GoTo myerror
    CopyRange.Copy wbDestination.Worksheets("Myyra").Range("d4")
    
'close & save
    'wbDestination.Close True
      
myerror:
    Application.ScreenUpdating = True
    If Err > 0 Then MsgBox (Error(Err)), 48, "Error"
End Sub

Change the copy sheet name shown in RED as required.

Dave


Dave,
Yes! It does work! Thank you very much! I appreciate it!
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,466
Members
449,086
Latest member
kwindels

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