+ Reply to Thread
Results 1 to 7 of 7

Macro to Create Folder\Sub Folder\SubFolder\

Hybrid View

  1. #1
    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: Macro to Create Folder\Sub Folder\SubFolder\

    Try this:
    Private Sub CommandButton1_Click()
        Dim sDir As String
        
        sDir = Range("B3").Text
        
        If Len(Dir(sDir, vbDirectory)) Then
            MsgBox ("Directory already exists")
        Else
            MsgBox IIf(MakeDir(sDir), "Directory created", "Failed")
        End If
    End Sub
    
    Function MakeDir(ByVal sDir As String) As Boolean
        ' shg 2008
        ' Returns True if the directory sDir exists or is successfully created
    
        Static sPS  As String
        Dim astr()  As String
        Dim iLvl    As Long
    
        If Len(Dir(sDir, vbDirectory)) = 0 Then
            If Len(sPS) = 0 Then sPS = Application.PathSeparator
    
            If Right(sDir, 1) = sPS Then sDir = Left(sDir, Len(sDir) - 1)
            astr = Split(sDir, sPS)
    
            ' MkDir will fail if sDir contains characters 0-31
            ' or any of the characters ' < > : " / \ | ? *
            ' or if the drive does not exist
            
            sDir = ""
            On Error Resume Next
            For iLvl = 0 To UBound(astr)
                sDir = sDir & astr(iLvl) & sPS
                If Len(Dir(sDir, vbDirectory)) = 0 Then MkDir sDir
                If Err.Number <> 0 Then
                    Err.Clear
                    Exit Function   '---------------------------------------------->
                End If
            Next iLvl
        End If
        
        MakeDir = True
    End Function
    Last edited by shg; 08-22-2009 at 11:19 AM.
    Entia non sunt multiplicanda sine necessitate

  2. #2
    Registered User
    Join Date
    02-08-2009
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    80

    Re: Macro to Create Folder\Sub Folder\SubFolder\

    Thank you very much works a treat.

    Many Thanks
    Last edited by shg; 08-22-2009 at 12:38 PM. Reason: deleted spurious quote

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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