Dynamic save path VBA

Riyen

New Member
Joined
Mar 7, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
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
 

Attachments

  • macro.PNG
    macro.PNG
    7.5 KB · Views: 19

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,214,920
Messages
6,122,262
Members
449,075
Latest member
staticfluids

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