Error Creating A Folder With VBA

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,616
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Consider this code that I am using to create a folder on my D: drive.


Rich (BB code):
mntxt = MonthName(Month(inq_date))
    daytext = WeekdayName(Weekday(inq_date), True)
    crtyr = Year(Now)
    filePath = distpath & crtyr & "\" & mntxt & "\" & Format(Day(inq_date), "00") & " " & UCase(daytext) & "\"
    nfn = "WS " & Format(inq_date, "dd-mmm-yy") & ".xlsx"
    
    If FolderExists(filePath) = False Then
        ui1 = MsgBox("Distributables file for " & Format(inq_date, "dddd mmm-dd") & " does not exist." & Chr(13) & "Create now?", vbInformation + vbYesNo, "Distributables Folder")
            If ui1 = vbYes Then
                MkDir filePath
            Else
                Exit Sub
            End If
    Else


Here are some values:
mntxt = "May"
daytext = "Fri"
crtyr = "2023"
filepath = "D:\WSOP 2020\Distributables\2023\May\12 FRI\" --- The path in green already exists.
nfn = "WS 12-May-23.xlsx"

The line in red is giving me a "Path not found Error". Am I using this code wrong?
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
You need to create the parent folder before you create a child folder.

Call this function with the full path and it will create the folders that don't already exist and in the right order.

VBA Code:
Public Sub CallsubCreateFolders()
    
' Example call'

' On my drive the Dump folder already exists.

    Call subCreateFolders("C:\Dump\aaa\bbb\ccc\ddd")
    
End Sub

Public Sub subCreateFolders(strFilePath As String)
Dim arrFolders() As String
Dim i As Integer
Dim strPath As String

    arrFolders = Split(strFilePath, "\")
    
    strPath = arrFolders(0)
    
    On Error Resume Next
    For i = 1 To UBound(arrFolders)
        strPath = strPath & "\" & arrFolders(i)
        MkDir strPath
    Next i
    On Error GoTo 0

End Sub
 
Upvote 0
Solution
Thank you Herakles! Seems to be working.
I'm having a problem deleting a folder as well in this post. Same issue?
 
Upvote 0
You need to delete the contents of the folder first which may include files or subfolders or both.

This sub will do that.

Test it first on some dummy folders and files.

You need to make reference to the Microsoft Scripting Runtime library.

VBA Code:
Public Sub subDeleteAllFilesAndSubfolders(sFolderPath As String)
Dim oFSO As FileSystemObject
        
    If Right(sFolderPath, 1) = "\" Then
        sFolderPath = Left(sFolderPath, Len(sFolderPath) - 1)
    End If
                
    'Create FSO Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    
    'Check Specified Folder exists or not
    If oFSO.FolderExists(sFolderPath) Then
        
          'Delete All Files
          oFSO.DeleteFile sFolderPath & "\*.*", True
                        
          'Delete All Subfolders
          oFSO.DeleteFolder sFolderPath & "\*.*", True
          
     End If
    
End Sub
 
Upvote 1
Thank you Herakles for sharing this code with me. It was very helpful.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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