Move file using VBA

TheAntisocial

New Member
Joined
Sep 7, 2009
Messages
33
Office Version
  1. 2013
Platform
  1. Windows
Hi all.

I have the following code.

Code:
Sub MoveFile()

Dim dFolderPath As String
Dim dFilePath As String


dFolderPath = Range("FolderPath").Value
dFilePath = Range("FilePath").Value


FileCopy "U:\CS Data\01 Live\2014-2015\Calendars\Cust Serv Calendar.lnk", "dFilePath"


Shell "C:\WINDOWS\explorer.exe """ & dFolderPath & "", vbNormalFocus


End Sub

I want to copy a file from folder 1 to folder 2 and then open folder 2.

Folder 2 will change depending on the user as it's linked to their payrole, hence the use of the Dims and not just the filepath.

The code runs with no error messages, and folder 2 opens, but the file is not being copied.

If I purposely make the filepath wrong then it errors saying it can't find the file.
If I don't use dFilePath and paste the cell contents/filepath it refers to directly into the code then in it copies the file.

Can anyone see where im going wrong? I'm assuming the problem is with dFilepath but i can't work out what.

All help is very much appreciated.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
"dFilePath"

should be

dFilePath

(no quotes)

also dfilepath should contain the full path and name for FileCopy

so maybe you mean

FileCopy "U:\CS Data\01 Live\2014-2015\Calendars\Cust Serv Calendar.lnk", dFolderPath & iif(right(trim(dFolderPath),1)<>"\","\","") & dfilepath

(also to do an actual move rather than a copy you should Kill the source file after the copy )

Kill "U:\CS Data\01 Live\2014-2015\Calendars\Cust Serv Calendar.lnk"
 
Upvote 0
Thanks CharlesChuckieCharles, could've sworn i'd tried dFilePath without the quotes but i can't have, as it's now work a treat.

Thank you for your help :)
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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