How to Use VBA to copy a file?

  • Thread starter Thread starter David Portwood
  • Start date Start date
D

David Portwood

I have filename MyFile.xls in folder C:\A and I want to copy it to folder
C:\B in a macro. How can I do this? I looked up the Copy() and CopyFile()
methods in the Help files but there is no example for how to use them.
 
You needed the "FileCopy" help file...

Sub MakeCopy()
Dim SourceFile As String
Dim DestinationFile As String

SourceFile = "C:\A\Mush.xla"
DestinationFile = "C:\B\Sludge.xla"
FileCopy SourceFile, DestinationFile
End Sub
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


"David Portwood"
<[email protected]>
wrote in message
I have filename MyFile.xls in folder C:\A and I want to copy it to folder
C:\B in a macro. How can I do this? I looked up the Copy() and CopyFile()
methods in the Help files but there is no example for how to use them.
 
David,
CopyFile is part of Scripting, not a native part of VBA.

Name "C:\Test.xls" As "C:\Test2.xls"
FileCopy
Kill
MkDir

Whilst I cannot get any Help on "Name" in the VBA help or the Object
Browser, the others are there.

NickHK
 
Thanks for the quick help, guys. I'll try your suggestions in the morning at
work.
 
From VBA help:

The Name statement renames a file and moves it to a different directory or
folder, if necessary. Name can move a file across drives, but it can only
rename an existing directory or folder when both newpathname and oldpathname
are located on the same drive. Name cannot create a new file, directory, or
folder.
 
Thanks, I knew it must be there, but typing "Name" in help gives me 200+
entries and I couldn't see the wood for the trees.
Also, F1 in code takes you to Name property rather than the statement, so
it's out of context.

At least the OP knows now.

NickHK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top