Attempting to create folder six levels down, get Path/File access error (runtime error 75)

OaklandJim

Well-known Member
Joined
Nov 29, 2018
Messages
855
Office Version
  1. 365
Platform
  1. Windows
I am trying to use some code that I got from a Mr Excel post. I need to create a folder that is six levels down. Keep getting that Runtime error 75, Path/File access error. Code I am using is here. Might someone assist with correction(s) needed?

VBA Code:
'routine to create your directory path
Sub MakeDirectory(FolderPath As String)

Debug.Print "FolderPath = " & FolderPath

    Dim i As Integer
    
    Dim strPath As String

    Dim x As Variant

    x = Split(FolderPath, "\")
    
    For i = 0 To UBound(x) - 1
        strPath = strPath & x(i) & "\"
        
Debug.Print i + 1 & ". " & strPath

        'If Not FolderExists(strPath) Then MkDir strPath '<= Runtime error 75
    Next i

End Sub


'function to check if folder exist
Function FolderExists(FolderPath As String) As Boolean
    On Error Resume Next
    ChDir FolderPath
    If Err Then FolderExists = False Else FolderExists = True
End Function

and debug.print output is here.

FolderPath = C:\Users\Planner3\XXXXX\Order Book - General\Type1\
1. C:\
2. C:\Users\
3. C:\Users\Planner3\
4. C:\Users\Planner3\XXXXX\
5. C:\Users\Planner3\XXXXX\Order Book - General\
6. C:\Users\Planner3\XXXXX\Order Book - General\Type1\
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
If I uncomment the offending line of code

VBA Code:
If Not FolderExists(strPath) Then MkDir strPath

Debug.Print shows three folder levels and no new folder is created.

FolderPath = C:\Users\Planner4\XXXXX\Order Book - General\Type3\
1. C:\
2. C:\Users\
3. C:\Users\Planner4\
 
Upvote 0
Hello @OaklandJim, thanks for posting on the forum.

Please try this. Run test macro.
VBA Code:
Sub MakeDirectory(FolderPath As String)
  Dim i As Integer
  Dim strPath As String
  Dim x As Variant
  Dim myDrive As String
 
  x = Split(FolderPath, "\")
  myDrive = x(0) & "\"
  For i = 1 To UBound(x) - 1
    strPath = strPath & x(i) & "\"
    If Dir(myDrive & strPath, vbDirectory) = "" Then
      MkDir myDrive & strPath
    End If
  Next i
End Sub

Sub test()
  Call MakeDirectory("C:\Users\Planner3\XXXXX\Order Book - General\Type1\")
End Sub
Note:
The FolderExists function is not required 😉

--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Upvote 0
Dante. I really appreciate the assistance. I cannot get your code to work.

If I comment out the line with mkdir
VBA Code:
    If Dir(myDrive & strPath, vbDirectory) = "" Then
      'MkDir myDrive & strPath
    End If

I get this Debug.Print so I know that code loops the correct path elements.

C:\Users\Planner2\test\Order Book - General\Type1\
1. C:\Users\
2. C:\Users\Planner2\
3. C:\Users\Planner2\test\
4. C:\Users\Planner2\test\Order Book - General\
5. C:\Users\Planner2\test\Order Book - General\Type1\

If I uncomment that line I get that Runtime error 75 and I get this Debug.Print output

C:\Users\Planner2\test\Order Book - General\Type1\
1. C:\Users\
2. C:\Users\Planner2\
 
Upvote 0
I should have posted your code with the Debug.Print commands added...

VBA Code:
Sub MakeDirectory(FolderPath As String)

Debug.Print FolderPath

  Dim i As Integer
  Dim strPath As String
  Dim x As Variant
  Dim myDrive As String
 
  x = Split(FolderPath, "\")
  myDrive = x(0) & "\"
  For i = 1 To UBound(x) - 1
    strPath = strPath & x(i) & "\"
    
Debug.Print i & ". " & myDrive & strPath

    If Dir(myDrive & strPath, vbDirectory) = "" Then
      MkDir myDrive & strPath
    End If
  Next i
End Sub
 
Upvote 0
Another approach is to use recursion
VBA Code:
Function MakeDirectory(ByRef FSO As Object, FolderPath As String) As Boolean
    With FSO
        If .FolderExists(FolderPath) Then
            MakeDirectory = True
            Exit Function
        End If
        If Not .FolderExists(.GetParentFolderName(FolderPath)) Then
            MakeDirectory FSO, .GetParentFolderName(FolderPath)
        End If
        .CreateFolder FolderPath
        If .FolderExists(FolderPath) Then
            MakeDirectory = True
        Else
            MakeDirectory = False
        End If
    End With
End Function

VBA Code:
Sub Test()
    Dim FolderPath As String, FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    FolderPath = "C:\Users\Planner3\XXXXX\Order Book - General\Type1\"

    If MakeDirectory(FSO, FolderPath) Then
        MsgBox "Success!"
    Else
        MsgBox "Failure"
    End If
End Sub

Note that if you get a Runtime Error 70, it means you do not have permission to create subfolders in one of your specified folders. This is not an uncommon restriction for the C:\Users directory on corporate managed PCs
 
Upvote 0
If I uncomment that line I get that Runtime error 75 and I get this Debug.Print output
Try another path, for example:
C:\work\example1\test\some

Manually create the initial "work" folder and then run my macro.

To rule out permissions issues.
 
Upvote 0
Solution
DanteAmor. I set permissions for the User folder and then your code worked. Thank you so much. I really appreciate the assistance.
 
Upvote 1

Forum statistics

Threads
1,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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