I would like to save my file in the folder created by...excel macro.
The process of vba excel folder creation has been presented here:
Create a folder and sub folder in Excel VBA
Following it, I have assigned it to my situation:
Sub Createfolder ()
Dim fso As Object
Dim fldrtitle As String
Dim flrdrname As String
Dim fldrpath As String
Set fso = CreateObject("scripting.filesystemobject")
fldrtitle = Worksheets("Sheet1").Range("A2").Value
fldrname = "Pre-planning NBU " & fldrtitle
fldrpath = "H:\ProfileV2\Desktop\Pre planning NBU\ Alex list new\" & fldrname
If not fso.FolderExists(fldrpath) Then
fso.Createfolder (fldrpath)
End If
End Sub
Where the folder has been created. My way to saving the file, according to the previous query looks as follows:
Sub Save()
Dim name As String, Custom_Name As String
name = Worksheets("Sheet1").Range("A2").Value
Custom_Name = ThisWorkbook.Path & "\" & "NBU " & name & " - Opportunity list.xlsx"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=Custom_name, FileFormat:=51
End Sub
Now, I tried to combine them together at some point, where the way 1 was:
Sub Save()
Call Createfolder
Dim name As String, Custom_Name As String
name = Worksheets("Sheet1").Range("A2").Value
Custom_Name = ThisWorkbook.Path & Createfolder & "NBU " & name & " - Opportunity list.xlsx"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=Custom_name, FileFormat:=51
End Sub
Here I am getting error: Expected Function or variable
what I understood, as I cannot fetch the external macro inside the code.
Afterwards I tried another way:
Sub Savetofolder ()
Dim fso As Object
Dim fldrtitle As String
Dim flrdrname As String
Dim fldrpath As String
Dim name As String, Custom_Name As String
Set fso = CreateObject("scripting.filesystemobject")
fldrtitle = Worksheets("Sheet1").Range("A2").Value
fldrname = "Pre-planning NBU " & fldrtitle
fldrpath = "H:\ProfileV2\Desktop\Pre planning NBU\ Alex list new\" & fldrname
If not fso.FolderExists(fldrpath) Then
fso.Createfolder (fldrpath)
End If
'name = Worksheets("Sheet1").Range("A2").Value
Custom_Name = ThisWorkbook.Path & Createfolder & "NBU " & fldrtitle & " - Opportunity list.xlsx"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=Custom_name, FileFormat:=51
End Sub
I switched off the name
variable, because it's the same as fldrtitle
.
In the result my file is saved next to the created folder as per in the picture below:
Is there any chance to get it saved INSIDE this folder?