Error on copy from one workbook to another

Milan Cetic

New Member
Joined
Oct 9, 2020
Messages
8
Office Version
  1. 2019
Platform
  1. Windows
I'm trying to copy cells from one workbook to another (copyfrom.xslm and copyto.xlsx)
This is the code I'm using

Sub CopyToWorkbook()

Range("A1:D4").Copy Workbooks("copyto").Worksheets("Sheet1").Range("A5")

End Sub

When I execute the code I get the Runtime error '9': Subscript out of range.
It's like there is no copyto file although it is open. Makes no sense but this is what's happening. Any ideas?

Thanks!
 

Attachments

  • 2020-10-09 20_55_22-Microsoft Visual Basic for Applications - Copyfrom.xlsm - [Module1 (Code)].png
    2020-10-09 20_55_22-Microsoft Visual Basic for Applications - Copyfrom.xlsm - [Module1 (Code)].png
    37.9 KB · Views: 7
If you are still getting the RTE 9 Subscript out of range, then there are a few possible reasons.
1) One of the workbooks is not open, or is not open in the same instance of Excel.
2) One of the workbook names is wrong.
3) One of the sheet names is wrong.
4) You did not use the exact code I supplied in post#5.
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
So what I have changed last is file type of file to which I'm writing. It was .xlsx and now is .xls

This is how it looks now and it works
Workbooks("copyfrom.xlsm").Sheets("Sheet1").Range("A1:D4").Copy Workbooks("copyto.xls").Worksheets("Sheet1").Range("A5")
The question is WHY? I have tried everything with the same syntax but with "copyto.xlsx" and I'm still getting RTE 9
 
Upvote 0
If the file is an xls file you will get an error with Workbooks("copyto.xlsx") because there is no such workbook open.
 
Upvote 0
Sorry if I was not clear.
When I use syntax with .xlsx and open copyto.xlsx file, I get RTE 9.
When I use syntax with .xls and open copyto.xls everything works
 
Upvote 0
I have already pointed out the possible reasons for that error. Without being able to see exactly what your files & sheets are called, there is not much more I can say.
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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