'This code creates a folder, but it does not save the file in it. It shows an alert message that - the file name or path doesnt exist

This code creates a folder, but it does not save the file in it. It shows an alert message that the file name or path doesnt exist. Can anyone explain what is wrong with this last line of code

    startPath = "C:\Users\OsmonBek\Documents\macros"
    myName1 = ActiveSheet.Range("A1").Text


    Dim folderPathWithName As String
    folderPathWithName = startPath & "\" & myName1

    If Dir(folderPathWithName, vbDirectory) = vbNullString Then
        MkDir folderPathWithName
    Else
    End If
    ' Save File
    ActiveWorkbook.SaveAs Filename:= _
    "folderPathWithName & \legend F22 A&P report " & Format(Now(), "DD-MMM-YYYY") & ".xlsx" _
    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False


Solution 1:[1]

You have your quote characters wrong. Probably you mean folderPathWithName & "\legend F22 A&P report " so you get the content of the path variable into the filename.

Write the filename into an intermediate variable, that helps finding such errors:

' Save File
Dim newFileName As String
newFileName = folderPathWithName & "\legend F22 A&P report " & Format(Now(), "DD-MMM-YYYY") & ".xlsx" 

ActiveWorkbook.SaveAs Filename:= newFileName _ 
                    , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 FunThomas