Save Excel Workbook into Created Folders

Cherrie_01

New Member
Joined
May 31, 2016
Messages
3
Hello,

I found a code that I would like to use to create a directory. In the each folder of the directory, I would like to save an excel file with the same name to the corresponding folder. The code creates a folder and file of my current workbook using the name listed in my reference cell however, I cannot get the code to work on a range of cells in excel. How can I get this code to work on a list of cells starting at A2? I don't have a defined range as this will be different for everyone that will be using this macro.

Any help would be greatly appreciated.

Here is the code that I am using:

Sub Macro1()
Dim strFilename, strDirname, strPathname, strDefpath As String
On Error Resume Next
strDirname = Range("A2").Value

strFilename = Range("A2").Value
strDefpath = Application.ActiveWorkbook.Path
If IsEmpty(strDirname) Then Exit Sub
If IsEmpty(strFilename) Then Exit Sub

MkDir strDefpath & "\" & strDirname
strPathname = strDefpath & "\" & strDirname & "\" & strFilename

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

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Cherrie_01,

You might give the following a try...

Code:
Sub Macro1()
Dim strFilename, strDirname, strPathname, strDefpath As String
Dim r As Range
Dim LastRow As Long

'On Error Resume Next
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
strDefpath = Application.ActiveWorkbook.Path

For Each r In Range("A2:A" & LastRow)
    strDirname = r.Value
    strFilename = r.Value
    
    If IsEmpty(strDirname) Then Exit Sub
    If IsEmpty(strFilename) Then Exit Sub

    MkDir strDefpath & "\" & strDirname
    strPathname = strDefpath & "\" & strDirname & "\" & strFilename

    ActiveWorkbook.SaveAs Filename:=strPathname & ".xlsm", _
        FileFormat:=xlOpenXMLWorkbookMacroEnabled, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=False
Next r

MsgBox "Done!"
End Sub

Cheers,

tonyyy
 
Upvote 0
Woozers? LOL!

Glad it worked out, Cherrie_01.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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