Moving a file with VBA code

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have some code that looks for and opens a file from location A, processes
and closes the file.

I now need to move the file from location A to location B (with the same
name). I have read up on the posts here and have looked at Excel Help and
Ron de Bruin's website (http://www.rondebruin.nl/folder.htm), but it still
doesn't work. This is what I currently have.....

MyPath = "C:\RECEIVED\"
NewPath = "C:\PROCESSED\"
...........some code to put all the files in the path (MyPath) into an
array..........
Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
Filename = mybook.Name
newname = NewPath & Filename
...........some more code to do all the processing..........
mybook.Close savechanges:=False
Name mybook AS newname

and...no good. The file still remains in the original location and is not
moved. I have tried the above line to look like...FileCopy mybook.name,
newname and even FileCopy mybook.fullname, newname and still no good.

Any help is greatly appreciated.
 
Correction. By the time you get to the line of code where you rename the
file, the workbook would have already been closed and so mybook.name would
error out. It should be like this:

Name MyPath & Filename As newname
 
Thanks Vergel, but it didn't work either way. I'm going to attack this again
tomorrow. I think I'll just step through the code with the Locals window
open to show me where the file name is getting "stuck."

Again, thanks.
 
hmmn.. that worked for me.. perhaps double check that a file with the same
name doesn't already exist in the destination folder... good luck.
 
Vergel,
Today, it worked. Probably to much processing yesterday (my brain not
functioning fully, crosslinked files (and my eyes), unrefreshed folder views
and all that stuff). But it works with the following code...

Again, thanks.
 

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

Combining two ideas? 3
Open CSV File 3
What is wrong with this code? 3
NEED HELP!!!! 1
help with merging worksheets 3
Error 461-Method or data member not found 3
Windows XP vba code help 10
Open files in order 11

Back
Top