You can use VB's Name..As statement to do that... you have to provided full paths to old and new filenames (this command allows you to move files to different directories as long as those directories already exist). For what you asked, here is a generic example...
GivenLocation = "c:\temp\" 'note the trailing backslash
OldFileName = "SomeFileName.xls"
NewFileName = "DifferentFileName.xls"
Name GivenLocation & OldFileName As GivenLocation & NewFileName
The parts in bold lettering are the required syntax of this command.
PrevMonth = DateAdd("m", -2, Date)
Saved_File = TARGET_DIR & "Trial Count " & Format(PrevMonth, "mm - yyyy")
BackUp_File = TARGET_DIR & Year(PrevMonth) & " Archive\" & "Trial Count " & Format(PrevMonth, "mm - yyyy")
If Dir(Saved_File & ".xls") <> "" Then
Name Saved_File & ".xls" As BACKUPFILE & ".xls"
ElseIf Dir(Saved_File & ".orig.xls") <> "" Then
Name Saved_File & ".orig.xls" As BACKUPFILE & ".orig.xls"
End If
What value is assigned to TARGET_DIR? What version of Windows are you using?I ran the code and the file disappeared from the original folder, but does not appear in the destination folder. I tried again and the code stops, indicating the file already exists...but it doesn't.
Code:PrevMonth = DateAdd("m", -2, Date) Saved_File = TARGET_DIR & "Trial Count " & Format(PrevMonth, "mm - yyyy") BackUp_File = TARGET_DIR & Year(PrevMonth) & " Archive\" & "Trial Count " & Format(PrevMonth, "mm - yyyy") If Dir(Saved_File & ".xls") <> "" Then Name Saved_File & ".xls" As BACKUPFILE & ".xls" ElseIf Dir(Saved_File & ".orig.xls") <> "" Then Name Saved_File & ".orig.xls" As BACKUPFILE & ".orig.xls" End If
What value is assigned to TARGET_DIR? What version of Windows are you using?
Well, I'm glad you got the file back doing that... it shows the Name..As statement actually does work (although I do not know why you could not find or see the moved file). I should have asked you this in my first message... what did you assign to BACKUPFILE?TARGET_DIR = "O:\xxxx\xxxxxxx\xxxxx\Trial Count\"
I am using Windows XP here at work.
I was able to reverse the NAME AS line to recover the file back to the original directory.
what did you assign to BACKUPFILE?
Well, I'm glad you got the file back doing that... it shows the Name..As statement actually does work (although I do not know why you could not find or see the moved file). I should have asked you this in my first message... what did you assign to BACKUPFILE?