• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

creation of Sub Folder and Super Sub Folders under Main Folder path

bhaskarjdk

New Member
Need help for Excel VBA code for creation of Sub Folder and Super Sub Folders under Main Folder path as shown in attached Excel workbook
 

Attachments

  • Filing.xlsb
    19.3 KB · Views: 9
A sample in this thread :​
Or just create the folders in the sub level order via MkDir statement so without the previous link trick​
but just checking before if a folder already exists via Dir function …​
 
A sample in this thread :​
Or just create the folders in the sub level order via MkDir statement so without the previous link trick​
but just checking before if a folder already exists via Dir function …​
If root directory is available and access is available to the user running the command then MkDir doesn't require folder existence check.

If the specified folder already exists then the command simply won't do anything and won't affect any existing data e.g. files or sub-directories which are existing. If path does not exist then it will create all directories which are not existing.
 
shrivallabha, did you try at least ? MkDir raises an error when the folder already exists !​
As MkDir raises an error as well when one of the parents folder does not exist​
so the reason why it's better to use the easy function MakeSureDirectoryPathExists when creating multiple sub levels at once …​
 
According to the attachment a beginner level starter VBA demonstration :​
Code:
Sub Demo1()
    For Each V In ["B"&{1,9,11,13,15,17,19}]
        If Dir(Range(V), 16) <> "." Then MkDir Range(V)
    Next
End Sub
Do you like it ? So thanks to click on bottom right Like !​
 
it's better to use the easy function MakeSureDirectoryPathExists when creating multiple sub levels at once
So from the deeper sub folders level starting with cell B11 even if parent folders B1 & B9 do not yet exist :​
Code:
Private Declare PtrSafe Function MakeSureDirectoryPathExists% Lib "imagehlp.dll" (ByVal DirPath$)

Sub Demo2()
    For R% = 11 To 19 Step 2:  E% = E% + 1 - MakeSureDirectoryPathExists(Cells(R, 2)):  Next
    Application.Speech.Speak IIf(E, "Error!", "Done!"), True
End Sub
You should Like it !​
 
shrivallabha, did you try at least ? MkDir raises an error when the folder already exists !​
As MkDir raises an error as well when one of the parents folder does not exist​
so the reason why it's better to use the easy function MakeSureDirectoryPathExists when creating multiple sub levels at once …​
I have tested and therefore commented. However, we probably are referring to two different approaches. Mine is mkdir through cmd and you are approaching through VBA.

Following line through cmd works fine as I have mentioned!
Code:
RetVal = Shell("cmd.exe /c mkdir " & Chr(34) & FldPath & Chr(34), 1)
 
Ok as now this is better explained with your DOS MkDir command versus my posts #2 & 5 VBA MkDir statement.​
According to the attachment with the DOS command :​
Code:
For R% = 11 To 19 Step 2:  Shell "cmd.exe /c mkdir """ & Cells(R, 2) & """", 0:  Next
So 3 ways solving this thread with 2 under Windows only …​
 
Back
Top