View Full Version : How to create a file?
prabhafriend
10-14-2010, 06:30 AM
I want to copy an xls file and rename it? I can use the filecopy statement but It needs the destination file to be created first. But How can I create a file through vba and rename. Kindly help. I want the easiest possible way. I think it will be a one liner. Thank you.
Hi there,
I am not sure I understand what you are asking. Does this do the trick?
Option Explicit
Sub exa()
Dim FilePath As String
'// Change path or paths to suit //
FilePath = ThisWorkbook.Path & "\"
FileCopy FilePath & "Test.xls", FilePath & "TestCopy.xls"
End Sub
prabhafriend
10-14-2010, 07:13 AM
GTO, How to create that TestCopy.xls through vba?
I must be missing something. Did you try the code?
prabhafriend
10-14-2010, 08:09 AM
FileCopy Statement won't create the file but replace the bytes of the Source with the Destination. It wont create the "Destination File"
You said you want to "...copy an xls file and rename it..."
Are you actually wanting to create a new/blank file?
Kenneth Hobs
10-14-2010, 11:02 AM
FileCopy was not designed to copy the current file for some odd reason.
Before doing something like that, you should probably save your file.
Your logic is reversed. A copy of the source is put into the destination.
When you copy a file, you also need to consider what to do if the file exists already.
With no error checking:
Sub t()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile ThisWorkbook.FullName, ThisWorkbook.Path & "\" & "TestCopy.xls", True 'overwrite
Set fso = Nothing
End Sub
prabhafriend
10-14-2010, 11:57 PM
Thank you kenneth
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.