Copy file and next instruction

tech2

New Member
Joined
Mar 8, 2011
Messages
30
Hi,

Below is a macro that copies a file. I'm also trying to include some additional instructions after the file is copied but I get a 'Run-time error 9' 'subscript out of range' on the 'Sheets("Report").Select', line.

I also would like some help adding additional code on file copy that would copy the latest file by date and time. The file name is always the same but the extension is different and it could be created several times a day.

Sub CopyFile()
'
' CopyFile Macro
'
FileCopy "C:\Documents and Settings\user\My Documents\Folder\Report_6.xls", "C:\Documents and Settings\user\Desktop\Folder\Report_6.xls"
Sheets("Report").Select
Sheets("Report").Name = "Sheet1"
ActiveWindow.SmallScroll ToRight:=2
Range("M1").Select
Selection.ClearContents
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
At the point when this line

'Sheets("Report").Select'

runs, do you actually have a sheet called "Report" ?
 
Upvote 0
Yes, the workbook is named 'Report' and the sheet is named 'Report'. The code is renaming the sheet to 'Sheet1'.
 
Upvote 0
Well I tested this myself in a few different scenarios and the only way I could get it to return that error message on that line was if I didn't have a sheet called "Report".

Is it possible that the sheet is called "Re port" or "Report " or something similar ?
Is it possible that, if you are re-naming sheets, the sheet is not actually called Report at the point that that code line runs ?
 
Upvote 0
As mentioned, you need to be sure that this sheet actually exists in the active workbook.

You may need to add this line before your code...
Code:
Windows("Book 1").Activate 'Substitute "Book 1" with the workbook name  'that contains this 'Results' sheet...

You can also check the names of all sheets that exist in the active workbook by running the following code...

Code:
Public Sub Test()
 
'Print the names of all sheets in the active workbook.
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Sheets
Debug.Print (sht.Name)
Next
 
End Sub
 
Upvote 0
Actually I'm embarrassed to say that you are correct the Book name is 'Report_6'.
I added the Windows("Report_6.xls").Activate, code and still got the same 'error 9 subscript out of range'.
The Copy file code works Great, it copies the workbook to the desktop folder. The code fails when it gets to renaming the sheet called 'Report_6'.
 
Upvote 0
The sheet needs to exist in your active workbook. If it doesn't exist then your code will fail at 'Sheets("Report").Select'.

When you get the error message type in your immediate window:

Code:
?Activeworkbook.Name

Again, you just need to be sure what workbook you are currently in...

Code:
Public Sub Test()
'Works fine, provided the sheet 'Prev Name' exists in your active workbook
Sheets("Prev Name").Select
Sheets("Prev Name").Name = "Another Name"
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,552
Messages
6,179,486
Members
452,917
Latest member
MrsMSalt

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