1

I have several files that need to be distributed into respective folders. How can I check to see if a folder exists with a dynamic name using an excel VBA macro?

I split a single workbook into several by the various worksheets within it. I believe it would be easiest if the macro used the name of each sheet in the original workbook to check for the existence of that folder. That way it's dynamic and I don't have to worry about coding it to search for each folder, as the data source continues to grow and need additional worksheets. I already have a code for searching for the folder, I just need to understand how to write it so that its dynamic.

Dim Path As String
Dim Folder As String
Dim Answer As VbMsgBoxResult
Dim NewPath As String
NewPath = ActvieWorkbook.Sheets.Name
Path = "C:\Test" & NewPath
Folder = Dir(Path, vbDirectory)
For Each sheetz0r In ActiveWorkbook.Sheets
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
End If
Next

In the code I have written, I just need the "NewPath =" line adjusted so that it will search for the sheet names.

5
  • Tools > References, then locate and add the Microsoft Scripting Runtime library. Your life will be much, much easier with a Scripting.FileSystemObject. Commented May 15, 2019 at 19:01
  • That said, NewPath looks like it needs to be re-assigned in the For Each sheetz0r loop body? NewPath = sheetz0r.Name? Commented May 15, 2019 at 19:12
  • I tried the NewPath = sheetz0r.Name In a previous iteration however I receive an error code that an object is required. Commented May 15, 2019 at 20:21
  • "Object Required" on that value assignment would have been thrown if you did Set NewPath = sheetz0r.Name - only objects can be assigned with the Set keyword Commented May 15, 2019 at 20:36
  • Thank you. It works great now. Commented May 15, 2019 at 21:58

2 Answers 2

1

Move the Path and Folder assignments inside the loop body, and replace & NewPath with & sheetz0r.Name - not sure what ActiveSheet.Sheets.Name is supposed to be, the Sheets collection class doesn't have a Name member.

I'd restructure things a bit, remove the redundant variables, and move declarations closer to their usage. I think what you mean to do is something like this?

Dim sheet As Worksheet
For Each sheet In ActiveWorkbook.Worksheets

    Dim Path As String
    Path = Dir("C:\Test" & sheet.Name, vbDirectory)

    If Path = vbNullString Then
        If MsgBox("Path does not exist. Would you like to create it?", vbYesNo, "Create Path?") = vbYes Then
            VBA.FileSystem.MkDir Path
        Else
            Exit For
        End If
    End If
Next

That said, verifying whether a folder exists, and creating a new one, is much simpler/cleaner using a FileSystemObject from the Scripting library - I would also abstract away the prompting part into its own function:

With New Scripting.FileSystemObject
    Dim sheet As Worksheet
    For Each sheet In ActiveWorkbook.Worksheets

        Dim Path As String
        Path = "C:\Test\" & sheet.Name

        If Not .FolderExists(Path) Then
            If ConfirmCreateFolder(Path) Then
                .CreateFolder Path
            Else
                Exit For
            End If
        End If

    Next
End With
Private Function ConfirmCreateFolder(ByVal Path As String) As Boolean
    Dim prompt As String
    prompt = "Folder '" & Path & "' does not exist. Would you like to create it?"
    ConfirmCreateFolder = (MsgBox(prompt, vbYesNo, "Create Folder?") = vbYes)            
End Function
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Sorry my code was so strange with the ActiveSheet.Sheets.Name I meant to take that out before posting. This helps a lot.
My Issue has been solved. Sadly I do not have enough credits to upvote the answer.
@TonyJeffs not until you get 15 points. But you can still mark an answer as "accepted" by clicking the hollow checkmark next to the voting buttons. That'll give you +2 already =)
0

Try this

Sub CheckFolder()
    Dim Path As String
    Dim Folder As String
    Dim Answer As VbMsgBoxResult
    Dim NewPath As String
    Dim scripObj As New Scripting.FileSystemObject


    Path = "C:\Test\"
    For Each sheetz0r In ActiveWorkbook.Sheets

    If Not scripObj.FolderExists(Path & sheetz0r.Name) Then
        Answer = MsgBox("Path does not exist. Would you like to create it?", vbYesNo, "Create Path?")
        Select Case Answer
            Case vbYes
                scripObj.CreateFolder (Path & sheetz0r.Name)
            Case Else
                Exit Sub
        End Select
    End If
    Next
End Sub

1 Comment

Thank you for your insight!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.