Copy file and rename sheet tab

tech2

New Member
Joined
Mar 8, 2011
Messages
30
Hi,

Below is the code that I use to copy an excel file to another folder, which works fine.
My question is: how can I rename the sheet 'Report_6' to 'Sheet1' after it has been copied?
This has to be done when the workbook also named 'Report_6' is closed.

Thx
Tech2

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"
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You have to open the file in order to rename the sheet.

Try:

Code:
Sub test1()
'
' CopyFile Macro
'
dim wkBk as workbook
set wkBk = workbooks.open("C:\Documents and Settings\user\My Documents\Folder\Report_6.xls")
 
wkbk.sheets("Report_6").name="Sheet1"
 
wkbk.saveas "C:\Documents and Settings\user\Desktop\Folder\Report_6.xls"
 
wkbk.close true
 
End Sub

Code untested and doesn't work in Excel 2007+... the SaveAs requires additional info in those versions. If using there, try recording the SaveAs with the macro recorder.
 
Upvote 0
Thanks taigovinda,

Your code works Great!

The only thing is: there is a warning message that comes up confirming if I want to replace the file. Is there a line of code that would keep this message from displaying?
 
Upvote 0
Yes, try this before and after the saveas line:

Code:
application.displayalerts = false
''''save as lne of code
application.displayalerts = true

actually... displayalerts I suppose is less specific and though I've not used it (and can't test because my Excel is tied up processing something), you may be better off to use application.alertbeforeoverwriting in this type of situation.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,248
Members
452,900
Latest member
LisaGo

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