Hello Guys,
I am really new to VBA, so i need some help.
I've created a macro which creates new folders and new excel files (from a template) based on column values in a list.
Currently the macro saves the generated workbooks to a specific folder.
I want it to save each generated workbooks into the folder what was generated previously in the same cycle. (the list file has the save location paths in column C)
So it creates Folder1, then it should save File1 into it, then same with Folder2, File2...etc.
I am struggling, becasue i cannot use a range in the saveAs function as a save location path. It only allows me to use strings, but i don't know how to use a variable which picks up the string value from the list workbook.
I've attached a picture about the list workbook.
Could you please help me?
Sub MakeFolders()
Const strSavePath As String = "C:\Users\fekettma\Desktop\Makro\"
Const strTemplatePath As String = "C:\Users\fekettma\Desktop\Makro\tmp.xlsx"
Dim maxRows, maxCols, r, c As Integer
Dim rngNames As Range
Dim Rnge As Range
Dim Rng As Range
Dim wkbTemplate As Workbook
With ThisWorkbook.Worksheets("Tabelle1")
Set rngNames = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With
Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)
Set Rng = ThisWorkbook.Worksheets("Tabelle1").Range("B2:B4").Cells
Set Path = ThisWorkbook.Worksheets("Tabelle1").Range("C2:C4").Cells
maxRows = Rng.Rows.Count
maxCols = Rng.Columns.Count
For c = 1 To maxCols
r = 1
Do While r <= maxRows
For Each Rnge In rngNames.Cells
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))
With wkbTemplate.Worksheets("Tabelle1")
.Range("A1").Value = Rnge.Value
End With
'THIS IS WHERE I NEED SOME HELP
wkbTemplate.SaveAs strSavePath & Rnge.Value
On Error Resume Next
End If
r = r + 1
Next Rnge
Loop
Next c
End Sub
I am really new to VBA, so i need some help.
I've created a macro which creates new folders and new excel files (from a template) based on column values in a list.
Currently the macro saves the generated workbooks to a specific folder.
I want it to save each generated workbooks into the folder what was generated previously in the same cycle. (the list file has the save location paths in column C)
So it creates Folder1, then it should save File1 into it, then same with Folder2, File2...etc.
I am struggling, becasue i cannot use a range in the saveAs function as a save location path. It only allows me to use strings, but i don't know how to use a variable which picks up the string value from the list workbook.
I've attached a picture about the list workbook.
Could you please help me?
Sub MakeFolders()
Const strSavePath As String = "C:\Users\fekettma\Desktop\Makro\"
Const strTemplatePath As String = "C:\Users\fekettma\Desktop\Makro\tmp.xlsx"
Dim maxRows, maxCols, r, c As Integer
Dim rngNames As Range
Dim Rnge As Range
Dim Rng As Range
Dim wkbTemplate As Workbook
With ThisWorkbook.Worksheets("Tabelle1")
Set rngNames = .Range("A2", .Range("A" & Rows.Count).End(xlUp))
End With
Set wkbTemplate = Application.Workbooks.Open(strTemplatePath)
Set Rng = ThisWorkbook.Worksheets("Tabelle1").Range("B2:B4").Cells
Set Path = ThisWorkbook.Worksheets("Tabelle1").Range("C2:C4").Cells
maxRows = Rng.Rows.Count
maxCols = Rng.Columns.Count
For c = 1 To maxCols
r = 1
Do While r <= maxRows
For Each Rnge In rngNames.Cells
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))
With wkbTemplate.Worksheets("Tabelle1")
.Range("A1").Value = Rnge.Value
End With
'THIS IS WHERE I NEED SOME HELP
wkbTemplate.SaveAs strSavePath & Rnge.Value
On Error Resume Next
End If
r = r + 1
Next Rnge
Loop
Next c
End Sub