• 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.

Pls help me with vba code to create folders and subfolders from excel cells

Hi, Hari7000!

As a new user you might want to (I'd say should and must) read this:
http://chandoo.org/forum/forums/new-users-please-start-here.14/

And regarding your issue, please elaborate a bit more, describe more precisely your requirement and eventually consider uploading a sample file (including manual examples of desired output if applicable), it'd be very useful for those who read this and might be able to help you, relieving the contributor of having to build a test file, if necessary. Thank you.

Regards!
 
I Need create folders and subfolders from excel list like below
Main Sub1 Sub2
SET 1 ABC AA
SET 2 DEF BB
SET 3 GHI CC like goes on till sub folders up to 10
 
Hi I don't know about subfolders but I use below code if I have to create multiple folders at once.
Code:
Option Explicit

Sub CreateFolders()

Dim Cell As Range
Dim MyPath As String
Dim MyRange As Range

MyPath = ThisWorkbook.Sheets(1).Cells(2, 1)
Set MyRange = Range(Cells(3, 3), Cells(3, 3).End(xlDown))
   
For Each Cell In MyRange
    On Error Resume Next
    MkDir MyPath & "\" & Cell.Value
    If Err.Number > 0 Then
        Cell.Offset(0, 1).Value = "Some error occurred"
    End If
Next Cell

End Sub

With Regards
Rudra
 
Hi I don't know about subfolders but I use below code if I have to create multiple folders at once.
Code:
Option Explicit
 
Sub CreateFolders()
 
Dim Cell As Range
Dim MyPath As String
Dim MyRange As Range
 
MyPath = ThisWorkbook.Sheets(1).Cells(2, 1)
Set MyRange = Range(Cells(3, 3), Cells(3, 3).End(xlDown))
  
For Each Cell In MyRange
    On Error Resume Next
    MkDir MyPath & "\" & Cell.Value
    If Err.Number > 0 Then
        Cell.Offset(0, 1).Value = "Some error occurred"
    End If
Next Cell
 
End Sub

With Regards
Rudra

Thank you Rudra
 
Thank you Rudra, i got a code from google search, Pls help me with inputbox for giving path for the location

Below is the code (Need inputbox for the highlited)

Option Explicit
Sub startCreating()
Call CreateDirectory(1, 1)
End Sub
Sub CreateDirectory(ByVal row As Long, ByVal col As Long, Optional ByRef path As String)
If (Len(ActiveSheet.Cells(row, col).Value) <= 0) Then
Exit Sub
End If
Dim sDir As String
If (Len(path) <= 0) Then
path = ActiveSheet.Cells(row, col).Value
sDir = "E:\" & path
Else
sDir = path & "\" & ActiveSheet.Cells(row, col).Value
End If
If (FileOrDirExists(sDir) = False) Then
MkDir sDir
End If
If (Len(ActiveSheet.Cells(row, col + 1).Value) <= 0) Then
Call CreateDirectory(row + 1, 1)
Else
Call CreateDirectory(row, col + 1, sDir)
End If
End Sub
Function FileOrDirExists(PathName As String) As Boolean
'Macro Purpose: Function returns TRUE if the specified file
' or folder exists, false if not.
'PathName : Supports Windows mapped drives or UNC
' : Supports Macintosh paths
'File usage : Provide full file path and extension
'Folder usage : Provide full folder path
' Accepts with/without trailing "\" (Windows)
' Accepts with/without trailing ":" (Macintosh)
Dim iTemp As Integer
'Ignore errors to allow for error evaluation
On Error Resume Next
iTemp = GetAttr(PathName)
'Check if error exists and set response appropriately
Select Case Err.Number
Case Is = 0
FileOrDirExists = True
Case Else
FileOrDirExists = False
End Select
'Resume error checking
On Error GoTo 0
End Function
 
Hi,
Are you looking for something like this?
Code:
sDir = "E:\" & InputBox("Enter your Path", " Path Please")
P.S. I have not tested your code.
With Regards
Rudra
 
Hi,
Are you looking for something like this?
Code:
sDir = "E:\" & InputBox("Enter your Path", " Path Please")
P.S. I have not tested your code.
With Regards
Rudra

Thank for reply, bascially what i want is any user enter the path to create folders in desired location

in this code need to change path in coding, insted of that when we run the program to pushbutton, it has to ask the path to create a folders.

if time permits pls run the code, some times its working, somtime not

Thanks & Regards,
Hari.
 
I had once started on this concept 2-3 years back and never really finished on it. But I think the folder making part works (it at least worked for the directory structure I wrote). You can do any change to the code or improve as needed.

Read instructions carefully before implementing. Column A should be drive letter and each subsequent level should be just one row below.

If you run the code as it is with structure I have typed in it will give you fair idea of what to expect.
 

Attachments

  • Folder_Utility_1.0.xlsm
    53.6 KB · Views: 119
Back
Top