VBA Run-time error '9' when attempting to copy from another worksheet

SimonR

New Member
Joined
Mar 14, 2022
Messages
23
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello,

At work we have a large worksheet of housing data that gets automatically updated every morning. I would like to create a working copy that is less cumbersome to navigate and which is automatically updated with information from the latest version of the original spreadsheet every time you open it.

The idea is on opening the copy you clear the contents and copy into it those of the original worksheet. Here's the code I've created so far (with the file names edited for security reasons):

VBA Code:
Sub UpdateWorksheet()
Range(Cells(2, 1), Cells(Application.WorksheetFunction.CountA(Range("a:a")), 25)).Select
Selection.ClearContents
Workbooks.Open "S:\R\A\T\T.xlsx"
Range(Cells(2, 1), Cells(Application.WorksheetFunction.CountA(Range("a:a")), 25)).Select
Selection.Copy
Windows("S:\R\A\T\T - Copy.xlsx").Activate
ActiveSheet.Paste
Columns("A:Y").EntireColumn.AutoFit
Range("A2").Select

The error in the thread title occurs whenever I try to open the worksheet that the updated information is supposed to go into (line 7) - I looked at a thread on Stack Overflow posted by a user with the same problem (Out of range error for Windows().Activate), and a variety of solutions were offered and I was unsure which one to use.

Can anyone explain why this error is occurring and how to rectify the code so that it does what I want it to do? The error originally occurred when I just used the file name in the Activate command, but as you can see it does not work even when I include the full path.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
This line should only have the name & extension of the file, not the entire pathWindows("S:\R\A\T\T - Copy.xlsx").Activate
 
Upvote 0
Solution
Ah yes, silly me - it didn't work when I just used the file name and extension because I used the wrong name, referring to a file called "Test" which wasn't open. Thanks!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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