+ Reply to Thread
Results 1 to 3 of 3

How to create multiple directories from cells using a macro or VBA

Hybrid View

riskmgr How to create multiple... 08-19-2011, 05:11 PM
tlafferty Re: How to create multiple... 08-19-2011, 05:28 PM
riskmgr Re: How to create multiple... 08-22-2011, 10:16 AM
  1. #1
    Registered User
    Join Date
    08-19-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2007
    Posts
    2

    How to create multiple directories from cells using a macro or VBA

    I have a very long list of numbers that I want to create directories for in a main shared network drive. I have found multiple HELP threads on the topic of how to create one directory and have made that work, but somehow need to be able to progress through the rows of cells in order to create multiple directories, in order to avoid having to create these directories manually. The VBA code I copied from a previous thread is as follows:
    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

    As mentioned, this works to create one directory, but, as per the attached sample worksheet, I need to be able to progress down the rows of cells from B3 to B4, etc.

    Any help/suggestions or appropriate code would be very much appreciated.
    Attached Files Attached Files
    Last edited by riskmgr; 08-22-2011 at 10:15 AM. Reason: Added Code Tags

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: How to create multiple directories from cells using a macro or VBA

    Welcome to the forum, riskmgr! Modified sub:
    Private Sub CommandButton1_Click()
        Dim sDir As String, lngLastRow As Long
        
        lngLastRow = Range("B65536").End(xlUp).Row
        
        'iterate through range
            For Each c In Range("B3:B" & lngLastRow)
                sDir = Range("B" & c.Row).Text
                If Len(Dir(sDir, vbDirectory)) Then
                    MsgBox ("Directory already exists")
                Else
                    MsgBox IIf(MakeDir(sDir), "Directory created", "Failed")
                End If
            Next
    End Sub
    See attached book for testing.
    Attached Files Attached Files
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Registered User
    Join Date
    08-19-2011
    Location
    Vancouver, Canada
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: How to create multiple directories from cells using a macro or VBA

    Thanks, tlafferty. Your resolution has been very helpful. Works like a charm!

    Cheers!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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