+ Reply to Thread
Results 1 to 7 of 7

Create new sub folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-01-2012
    Location
    NJ
    MS-Off Ver
    Excel 365
    Posts
    120

    Create new sub folder

    Hello,

    Does anyone know a quick code to create a sub folder under the parent directory listed in column A.
    I have about 600 different parent directories in column A and the new folder I need to create is in the corresponding row next to it in Column B. column B names are all different as well. I want to check if the new folder exists listed in column B already and if it doesn't create a new folder. If it does already exist, move on. Anyone know a quick way to do this?

    Thanks!!!

  2. #2
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Create new sub folder

    This will create all the sub paths necessary to fulfill the DestPath:
    Public Function CreatePath(ByVal DestPath As String) As Boolean
      Dim TempDir As String
      Dim ForePos As Integer
      Dim Term As Long
      Dim IsUNC As Boolean
      DestPath = Wack(Trim$(DestPath))
      IsUNC = DestPath Like "\\*"
      If IsUNC Then
        ForePos = InStr(3, DestPath, "\")
      Else
        ForePos = InStr(4, DestPath, "\")
      End If
      Term = 1
      Do While ForePos <> 0
        TempDir = Left$(DestPath, ForePos - 1)
        If Not (IsUNC And Term <= 2) Then
          On Error Resume Next
          Err = 0
          MkDir TempDir
          If Err <> 0 And Err <> 75 Then
            GoTo ErrorOut
          End If
          Err = 0
        End If
        ForePos = InStr(ForePos + 1, DestPath, "\")
        Term = Term + 1
      Loop
      CreatePath = True
      Exit Function
                     
    ErrorOut:
      MsgBox Error$, vbExclamation
      CreatePath = False
    End Function

  3. #3
    Forum Contributor
    Join Date
    11-01-2012
    Location
    NJ
    MS-Off Ver
    Excel 365
    Posts
    120
    Quote Originally Posted by cyiangou View Post
    This will create all the sub paths necessary to fulfill the DestPath:
    Public Function CreatePath(ByVal DestPath As String) As Boolean
      Dim TempDir As String
      Dim ForePos As Integer
      Dim Term As Long
      Dim IsUNC As Boolean
      DestPath = Wack(Trim$(DestPath))
      IsUNC = DestPath Like "\\*"
      If IsUNC Then
        ForePos = InStr(3, DestPath, "\")
      Else
        ForePos = InStr(4, DestPath, "\")
      End If
      Term = 1
      Do While ForePos <> 0
        TempDir = Left$(DestPath, ForePos - 1)
        If Not (IsUNC And Term <= 2) Then
          On Error Resume Next
          Err = 0
          MkDir TempDir
          If Err <> 0 And Err <> 75 Then
            GoTo ErrorOut
          End If
          Err = 0
        End If
        ForePos = InStr(ForePos + 1, DestPath, "\")
        Term = Term + 1
      Loop
      CreatePath = True
      Exit Function
                     
    ErrorOut:
      MsgBox Error$, vbExclamation
      CreatePath = False
    End Function
    Shouldn't this be a Sub and not a function? I can not get this to work

    Thanks

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create new sub folder

    @cyiangou
    I think there is error in this udf function
    Can you guide us how to use it?
    Thanks advanced
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Create new sub folder

    Suppose your data in column A and sub folders names in column B
    Try this code
    Sub CreateDirs()
        Dim Rng As Range, RW As Range, CL As Range
        Const RootFolder = "C:\ExcelForum"   'Change To Suit
        Set Rng = Range("A1:B5") 'Change To Suit
    
        For Each RW In Rng.Rows
            ChDir RootFolder
            For Each CL In RW.Cells
                On Error Resume Next
                If CL <> "" Then
                    MkDir CL
                    ChDir CL
                End If
                On Error GoTo 0
            Next
        Next
    End Sub

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Create new sub folder

    Here's what I use:

    Sub Demo()
      MakeDirs Intersect(Columns("A"), ActiveSheet.UsedRange).Value
    End Sub
    
    Sub MakeDirs(avsDir As Variant)
      ' shg 2008
      Dim vsDir         As Variant
    
      For Each vsDir In avsDir
        vsDir = CStr(vsDir)
        If Len(vsDir) Then
          Select Case MakeDir(vsDir)
            Case -1
              MsgBox Prompt:="MakeDirs: Directory  """ & vsDir & """ already exists", _
                     Title:="Oops!"
            Case 0
              MsgBox Prompt:="MakeDirs: Unable to create """ & vsDir & """", _
                     Title:="Oops!"
          End Select
        End If
      Next vsDir
    End Sub
    
    Function MakeDir(ByVal sDir As String) As Long
      ' shg 2008
      ' Returns -1 if sDir already exists
      '     1 if sDir is successfully created
      '     0 if sDir cannot be created
    
      Dim sPS           As String
      Dim astr()        As String
      Dim iLvl          As Long
    
      If Len(Dir(sDir, vbDirectory)) Then
        MakeDir = -1
    
      Else
        sPS = Application.PathSeparator
    
        If Right(sDir, 1) = sPS Then sDir = Left(sDir, Len(sDir) - 1)
        astr = Split(sDir, sPS)
        sDir = vbNullString
    
        ' MkDir will fail if sDir contains character codes 0-31
        ' or any of the characters ' < > : " / \ | ? *
        ' or if the drive does not exist,
        ' or if the user does not have permissions, so ...
        On Error GoTo Oops
    
        For iLvl = 0 To UBound(astr)
          sDir = sDir & astr(iLvl) & sPS
          If Len(Dir(sDir, vbDirectory)) = 0 Then MkDir sDir
        Next iLvl
    
        MakeDir = 1
      End If
    OuttaHere:
      Exit Function
    
    Oops:
      Err.Clear
      Resume OuttaHere
    End Function
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Valued Forum Contributor
    Join Date
    09-17-2012
    Location
    Johannesburg, South Africa
    MS-Off Ver
    Excel 2007
    Posts
    454

    Re: Create new sub folder

    @cyiangou
    I think there is error in this udf function
    Can you guide us how to use it?
    Just call it like this:
    CreatePath "C:\folder1\folder2\folder3"
    it should build all the levels if they don't exist.

    Shouldn't this be a Sub and not a function? I can not get this to work
    It's configured as a function if you want to get success/failure info from it, but just call it like a sub.

    It should work perfectly. This particular proc I have used unchanged for almost 20 years (as evidenced by my 2 space indentation).

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Loop Through Folder, Create Emails with Sub Folder Names in Subject, Attach files in sub
    By Rschwar23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2015, 10:06 AM
  2. Create FOLDER in subfoldesr and move folders with specific names into FOLDER
    By Amarjeet Singh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2015, 09:46 AM
  3. Code to create New Folder (if doesn't exist) and then Save Workbook to folder
    By jenhawley in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-10-2013, 03:09 PM
  4. Replies: 13
    Last Post: 06-04-2012, 02:18 PM
  5. create a compressed folder or zipped folder then copy workbooks to it.
    By Ironman in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-04-2012, 03:56 PM
  6. Create Folder with system date then save file to folder
    By cartotech81 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2009, 02:12 PM
  7. Replies: 6
    Last Post: 08-11-2006, 03:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1