Editing VBA code to create a new folder in a very specific location?

thefastlife

New Member
Joined
May 25, 2016
Messages
24
working with the following code. as of now, it just saves it as a new folder in the C drive.

i'd like to drill a bit deeper and have a relatively long location path.

is this possible? where would i input the exact path?

thanks!

Rich (BB code):
'Create new folder to save the new files in
    DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
    FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
    MkDir FolderName
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
MkDir FolderName


The FolderName string is the path that that folder will end up. It looks like the current code creates a folder in the same location that the workbook is in. If you wanted to change it to something exact and hard coded you would just need to put the location in quotes, like "C:\Test\MyNewSubFolder".

Just keep in mind that running the macro a second time will do nothing if that folder is still there.

For your reference:

Sourcewb.Path = The folder location that the opened workbook is located in.
Sourcewb.Name = The file name of the opened workbook.

Also, if you try to make a dynamic file location and want to test your string output, just add a single quote in front of the 'MkDir FolderName line so it becomes a comment. Then write out MsgBox (FolderName) on a new line. This will pop up a box and let you see what it's writing when you run the macro.

-Grummet
 
Upvote 0
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = Sourcewb.Path & "\" & Sourcewb.Name & " " & DateString
MkDir FolderName


The FolderName string is the path that that folder will end up. It looks like the current code creates a folder in the same location that the workbook is in. If you wanted to change it to something exact and hard coded you would just need to put the location in quotes, like "C:\Test\MyNewSubFolder".

Just keep in mind that running the macro a second time will do nothing if that folder is still there.

For your reference:

Sourcewb.Path = The folder location that the opened workbook is located in.
Sourcewb.Name = The file name of the opened workbook.

Also, if you try to make a dynamic file location and want to test your string output, just add a single quote in front of the 'MkDir FolderName line so it becomes a comment. Then write out MsgBox (FolderName) on a new line. This will pop up a box and let you see what it's writing when you run the macro.

-Grummet
thanks Grummet! would i not have to also update this piece too?

Rich (BB code):
    'Save the new workbook and close it
            With Destwb
                .SaveAs FolderName _
                      & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                        FileFormat:=FileFormatNum
                .Close False
            End With


also would the updated code look like this?

Rich (BB code):
DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
FolderName = "C:\VBA Testing\2016" & "\" & Sourcewb.Name & " " & DateString
MkDir FolderName
 
Last edited:
Upvote 0
scratch that above question. i figured it out and it was exactly as i suspected. worked great!

one "issue" however. i am working off a pivot table on my main file. when i run this code it completely changes the look of the formatting. i assume b/c in the master file it's driven by that pivot table.

the odd thing is, when i 'Move or Copy' each sheet individually/manually it preserves this formatting.

is there anything that can be done about that?
 
Upvote 0

Forum statistics

Threads
1,216,028
Messages
6,128,395
Members
449,446
Latest member
CodeCybear

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