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
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