Create new folder if folder does not exist

DanSMT

Board Regular
Joined
Sep 13, 2019
Messages
203
Office Version
  1. 2013
Platform
  1. Windows
I'm struggling with this one. It continually errors out when attempt to create the dir.

Location of error is last line MKDir partsubfolder

Any assistance would be greatly appreciated. Thanks in advance!

Code below;

VBA Code:
Dim sourceDir As String
Dim folder_exists As String
Dim partSubfolder As String

    'source directory
    sourceDir = "C:\Users\dans\documents\" & CustomerTB.Value & "\"
    partSubfolder = sourceDir & PartNoCB.Value & "\"
    
    'check if year subfolder exists, if it doesn't then create a new folder
    folder_exists = Dir(partSubfolder, vbDirectory)
    If folder_exists = vbNullString Then MkDir partSubfolder
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
What are the values which you are using in your partsubfolder path?
 
Upvote 0
What are the values which you are using in your partsubfolder path?
There are derived from the userform.

In this case partsubfolder would be = to the following

C:\user\dans\documents\Customer\120144-c-f\

Customer name is hidden.

Customer is derived from textbox on userform

part number is derived from textbox on userform
 
Upvote 0
It could be that you have some non-printing (invisible) characters in the ranges CustomerTB and PartNoCB.Value (besides white spaces). Try if this works. If not, also check for visible (printing) characters in those ranges' values which are not allowed to be used in a folder name.

VBA Code:
Sub Unknown_Name()

Dim sourceDir As String
Dim folder_exists As String
Dim partSubfolder As String

    'source directory
    sourceDir = "C:\Users\dans\documents\" & Remove_Stuff(CustomerTB.Value) & "\"
    partSubfolder = sourceDir & Remove_Stuff(PartNoCB.Value) & "\"
 
    'check if year subfolder exists, if it doesn't then create a new folder
    folder_exists = Dir(partSubfolder, vbDirectory)
    If folder_exists = vbNullString Then MkDir partSubfolder


End Sub

Sub Test__Remove_Stuff()
Debug.Print Remove_Stuff(Chr(8) & "  " & Chr(10) & "AA" & Chr(10) & "BE" & " ")
End Sub
Function Remove_Stuff(str As String)
'Remove_All_Non_Printing_Characters_Except_For_White_Spaces
'Important note from:  [URL='https://docs.microsoft.com/en-us/office/vba/api/excel.worksheetfunction.clean']WorksheetFunction.Clean method (Excel)[/URL]
'The Clean function was designed to remove the first 32 nonprinting characters
'in the 7-bit ASCII code (values 0 through 31) from text. In the Unicode character set,
'there are additional nonprinting characters (values 127, 129, 141, 143, 144, and 157).
'By itself, the Clean function does not remove these additional nonprinting characters.
Remove_Stuff = Application.WorksheetFunction.Clean(str)
End Function
 
Upvote 0
It could be that you have some non-printing (invisible) characters in the ranges CustomerTB and PartNoCB.Value (besides white spaces). Try if this works. If not, also check for visible (printing) characters in those ranges' values which are not allowed to be used in a folder name.

I still get the same error @ MkDir partSubfolder
 
Upvote 0
Okay, maybe try this alternative:
VBA Code:
Sub Test__CreateDirectory()

Dim sourceDir As String
Dim folder_exists As String
Dim partSubfolder As String

'source directory
sourceDir = "C:\Users\dans\documents\" & CustomerTB.Value & "\"
partSubfolder = sourceDir & PartNoCB.Value & "\"

Call CreateDirectory(partSubfolder)

End Sub

Sub CreateDirectory(strPath As String)
'https://stackoverflow.com/a/33671329

    Dim elm As Variant
    Dim strCheckPath As String

    strCheckPath = ""
    For Each elm In Split(strPath, "\")
        strCheckPath = strCheckPath & elm & "\"
        If Len(Dir(strCheckPath, vbDirectory)) = 0 Then MkDir strCheckPath
    Next
End Sub
 
Upvote 0
Solution
Okay, maybe try this alternative:
VBA Code:
Sub Test__CreateDirectory()

Dim sourceDir As String
Dim folder_exists As String
Dim partSubfolder As String

'source directory
sourceDir = "C:\Users\dans\documents\" & CustomerTB.Value & "\"
partSubfolder = sourceDir & PartNoCB.Value & "\"

Call CreateDirectory(partSubfolder)

End Sub

Sub CreateDirectory(strPath As String)
'https://stackoverflow.com/a/33671329

    Dim elm As Variant
    Dim strCheckPath As String

    strCheckPath = ""
    For Each elm In Split(strPath, "\")
        strCheckPath = strCheckPath & elm & "\"
        If Len(Dir(strCheckPath, vbDirectory)) = 0 Then MkDir strCheckPath
    Next
End Sub
Success!!

Next steps will be moving the file from the original folder to this folder. Key parts is making sure the original file is not copied and the original folder the file was in is empty.

Thank you so much for your assistance!!
 
Upvote 0
Success!!

Next steps will be moving the file from the original folder to this folder. Key parts is making sure the original file is not copied and the original folder the file was in is empty.

Thank you so much for your assistance!!
You're quite welcome. I guess you already had files in the subfolder?

But to answer your new question: (And of course, this code assumes that you don't have a \ at the end of sourcePath and destinationPath. But if they do, simply omit the two & "\" from the final line of code.)

VBA Code:
Sub Test__Move_This_File_From_This_Dir_to_This_Dir()

Dim sourcePath As String
Dim destinationPath As String
Dim fileName As String
sourcePath = "something"
destinationPath = "something"
fileName = "something" 'Include file extension, of course

Call Move_This_File_From_This_Dir_to_This_Dir("ff.txt", sourcePath, destinationPath)
End Sub

Sub Move_This_File_From_This_Dir_to_This_Dir(fileName As String, source_Directory As String, destination_Directory As String)
'https://stackoverflow.com/a/16943127

Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.MoveFile(source_Directory & "\" & fileName, destination_Directory & "\" & fileName)

End Sub
 
Upvote 0
You're quite welcome. I guess you already had files in the subfolder?

But to answer your new question: (And of course, this code assumes that you don't have a \ at the end of sourcePath and destinationPath. But if they do, simply omit the two & "\" from the final line of code.)

VBA Code:
Sub Test__Move_This_File_From_This_Dir_to_This_Dir()

Dim sourcePath As String
Dim destinationPath As String
Dim fileName As String
sourcePath = "something"
destinationPath = "something"
fileName = "something" 'Include file extension, of course

Call Move_This_File_From_This_Dir_to_This_Dir("ff.txt", sourcePath, destinationPath)
End Sub

Sub Move_This_File_From_This_Dir_to_This_Dir(fileName As String, source_Directory As String, destination_Directory As String)
'https://stackoverflow.com/a/16943127

Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.MoveFile(source_Directory & "\" & fileName, destination_Directory & "\" & fileName)

End Sub
TY!

I would assume both of these subs can be ran under the same function?
 
Upvote 0
I get an error on the last line.

What is the ff.txt for?
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

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