Copy file and paste file with filename stored as variable VBA

DouglasWicker

New Member
Joined
Aug 8, 2017
Messages
38
Afternoon All,

I am trying to copy a file from one location to another. Just use FileCopy right?
Well urmm no? The issue I have is that the file that is copied changes and therefore its name address and extension can change. Its full address is saved as a variable. I am trying to paste it in a folder without changing the name of the file and am strugging. Hopefully this code below will help explain what I require.

This works. However it requires me to manually change the name of the file when pasted into the destination folder, therefore is no good. Both the source address is a variable as is the destination folder address.

Code:
FileCopy "C:\Test\Test.xlsm", "C:\iamtestinghere\Test.xlsm"

What I need then is this, however it is not working, I get a 75 error, path/ file access error.

Code:
FileCopy "C:\Test\Test.xlsm", "C:\iamtestinghere\"

Anyone got any ideas?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Try this:
Code:
Sub SpecialCopy()

    Dim myFPName As String
    Dim myNewDir As String
    Dim myFName As String
    
'   Full name and path of original file
    myFPName = "C:\Test\Test.xlsm"
'   Name of new directory to copy to
    myNewDir = "C:\iamtestinghere\"
    
'   Get just file name from original file
    myFName = Mid(myFPName, InStrRev(myFPName, "\") + 1)
    
'   Copy file to new directory with same name
    FileCopy myFPName, myNewDir & myFName
            
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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