-
How to create a file?
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?
[vba]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[/vba]
Last edited by Aussiebear; 10-16-2010 at 02:41 AM.
Reason: Adjusted correct code tags for User
-
GTO, How to create that TestCopy.xls through vba?
-
I must be missing something. Did you try the code?
-
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?
-
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:
[VBA]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[/VBA]
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules