Move a single file to a different folder

VulkaJohn

New Member
Joined
Sep 25, 2014
Messages
17
I want to move a file from one folder to another, the macro fails in the last line, any help would be appreciated

Sub a_Move_File()
Dim Ans As String
Sub a_Move_File()
Dim OldFol As String
Dim NewFol As String
Dim filnam As String
Dim OldNam As String
Dim NewNam As String

'Source folder address in P3 in the form "C:\PaidNotEntered"
OldFol = Range("P3").Value

'Destination folder in p4
NewFol = Range("P4").Value

'Filename in the form "test.pdf"
filnam = ActiveCell.Value

' Concatenated Source folder and filename in the form "C:\PaidNotEntered\test.pdf"
OldNam = OldFol & filnam

' Concatenated Destination folder and filename
NewNam = NewFol & filnam

Name OldNam As NewNam

End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hi

This is a macro I've used for some time for just such a purpose. I got it from here but can't remember who from, kudos goes to them though...

In it's current for it'll loop through column A and rename it as column B. This can be a different directory as might be your need.

Code:
Sub rename()
On Error Resume Next
For i = 1 To Range("A1").End(xlDown).Row
oldfilename = Cells(i, 1).Value
newfilename = Cells(i, 2).Value


If Not Dir(oldfilename) = "" Then Name oldfilename As newfilename
Next
End Sub

Hopefully you can modify it for your own use.
 
Upvote 0
Hi

This is a macro I've used for some time for just such a purpose. I got it from here but can't remember who from, kudos goes to them though...

In it's current for it'll loop through column A and rename it as column B. This can be a different directory as might be your need.

Code:
Sub rename()
On Error Resume Next
For i = 1 To Range("A1").End(xlDown).Row
oldfilename = Cells(i, 1).Value
newfilename = Cells(i, 2).Value


If Not Dir(oldfilename) = "" Then Name oldfilename As newfilename
Next
End Sub

Hopefully you can modify it for your own use.

Thanks for that, Essentially this is what I am doing.

the line "Name oldfilename As newfilename" is where my macro falls over although it should work.
 
Upvote 0
Yes, it isn't hugely different. The one I gave you will continue after an error and it also checks if the destination exists. Leads me to ask if your destination folder exists, if not you could include a check... if destination folder exists go ahead and save, else create the folder then save.
 
Upvote 0
Yes, it isn't hugely different. The one I gave you will continue after an error and it also checks if the destination exists. Leads me to ask if your destination folder exists, if not you could include a check... if destination folder exists go ahead and save, else create the folder then save.

Problem solved!

Folders both exist BUT test filename mis-spelt I had tset.pdf not test.pdf

Thanks for pointing me in the right direction
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top