Sunday, June 21, 2020

check if a folder exists in a path and if not create the folder in that path on user prompt using vba

Sub Path_Exists()

Dim Path As String
Dim Folder As String
Dim Answer As VbMsgBoxResult

    Path = "C:\Users\allso\Desktop\excel_to_pdf"

    Folder = dir(Path, vbDirectory)
 
    If Folder = vbNullString Then

        Answer = MsgBox("Path does not exist. Would you like to create it?", vbYesNo, "Create Path?")

        Select Case Answer
            Case vbYes
                VBA.FileSystem.MkDir (Path)
            Case Else
                Exit Sub
        End Select

    Else

        MsgBox "Folder exists."

    End If

End Sub

No comments:

Post a Comment