Create Subfolder via macro

charlie_580

Board Regular
Joined
Feb 3, 2007
Messages
56
Hi

I'm using this code to save to a network drive.

Code:
Sub save()
 Dim strFilename, strDirname, strDirname2, strPathname, strDefpath As String
 On Error Resume Next ' If directory exist goto next line
 strDirname = Range("X2").Value ' New directory name
 strDirname2 = Range("C1").Value
 strFilename = Range("T1") & " " & Range("A6") 'New file name
 strDefpath = "N:\Returns\" 'Default path name
 If IsEmpty(strDirname) Then Exit Sub
 If IsEmpty(strDirname2) Then Exit Sub
 If IsEmpty(strFilename) Then Exit Sub

 MkDir strDefpath & strDirname
 strPathname = strDefpath & strDirname & "\" & strDirname2 & "\" & strFilename 'create total string

 ActiveWorkbook.SaveAs Filename:=strPathname, _
 FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="", WriteResPassword:="", _
 ReadOnlyRecommended:=False, CreateBackup:=False
 End Sub

If I remove the strDirname2 it will save in the folder as the filename. I need the sub folder to work though as there are 5 possible subfolders.

Any help greatly appreciated.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You need to also make the subfolder using this line:

Code:
MkDir strDefpath & strDirname & "\" & strDirname2

You should place it below the existing line:

Code:
MkDir strDefpath & strDirname
 
Upvote 0

Forum statistics

Threads
1,213,487
Messages
6,113,937
Members
448,534
Latest member
benefuexx

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