VBA for copy files to a directory

Octonet

New Member
Joined
Sep 5, 2020
Messages
27
Office Version
  1. 2019
Platform
  1. Windows
I have a workbook d:\private\test.xlsm with lots of tabs and data.
Each year this workbook is copied to an archive directory (d:\private\archive\"year") and cleared completely for the new year.
I made a separate workbook with a protected vba for clearing the complete workbook (the famous red button). But I want to automate the copying too.
In the red button workbook the desired year has to be entered in cell G10. The first thing the red button has to do is create a directory in the archive directory with the name of the G10 value.
After that it has to copy the test xlsm to that new directory. After that my clearing script will do the rest with the original test.xlsm.
The first part, before the clearing is my big problem.
Does anyone have a good solution ?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hello Dave,
That's no problem but...... Ik want a new directory to be created, and the name of that directory has to be the value of cell G10.
After that the workbook has to be saved to that new directory.Example : if G10 = 2020, a directory with name 2020 has to be created and the workbook has to be saved into that (G10 value) directory.
 
Upvote 0
You can use the MkDir statement

VBA Code:
MkDir "d:\private\archive\" & Range("G10").Value

Note: The Top level folders must already exist - only the last folder specified in your range will be created.



Dave
 
Upvote 0
Solution
Thank You very much Dave, this works perfectly.
I'm just beginning to learn, and there is a lot to be learned :)
 
Upvote 0
Creating that new directory works perfect.
So I was thinking to use the same way for copying the files to that newly created directory.
But I was wrong.
I tried :
FileCopy "d:\Private\test.xlsm", "d:\privat\Archive\" & Range("C10").Value

Here I got an access error on file or path.

What stupidity did I do ?
 
Upvote 0
I tried :
FileCopy "d:\Private\test.xlsm", "d:\privat\Archive\" & Range("C10").Value

Here I got an access error on file or path.

What stupidity did I do ?

assuming the path exists, looking at your code suggests you may have a typo

FileCopy "d:\Private\test.xlsm", "d:\private\Archive\" & Range("C10").Value

Dave
 
Upvote 0
I found the stupidity :)
It should be :
FileCopy "d:\Privaat\test.xlsm", "d:\privaat\Archive\" & Range("C10").Value & "\" & "test.xlsm"

Thanks for the help :)
Problems completely solved.
 
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,449
Members
448,966
Latest member
DannyC96

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