Consulting

Results 1 to 3 of 3

Thread: Help with Creating directories & subdirectories

  1. #1
    VBAX Regular
    Joined
    Sep 2008
    Location
    Sheffield
    Posts
    72
    Location

    Exclamation Help with Creating directories & subdirectories

    Hello, Advancing on creating directories to also creating sub directories. Below is the code for Creating a directory from a list in column A. (which works) to create the directories i list, but below that i have found a code which helps to make subdirectories in a way but putting them together isn't working for me too well. Now that i have a list of what the directories are going to be in column A i need to put a list of the 1st subdirectory in column b, then 2nd in C and so on. It must be possible I just need help as i am all new and learning lol. Thanks

    --------------------------------------------------------------
    [VBA]Public Sub ProcessData()
    Const ROOT_FOLDER As String = "C:\Documents and Settings\Administrator\My Documents\Other\Test\"
    Dim LastRow As Long
    Dim i As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    On Error Resume Next
    For i = 1 To LastRow

    MkDir ROOT_FOLDER & .Cells(i, "A").Value

    Next i
    On Error GoTo 0
    End With

    End Sub
    [/VBA]
    --------------------------------------------------------

    [VBA]Sub MakeDirectories()
    ' Declare variable for creating folders...
    Dim Dir1 As String, Dir2 As String, Dir3 As String
    ' Assign directories location and its name...
    Dir1 = "C:\MainFolder"
    Dir2 = Dir1 & "\SubFolder1"
    Dir3 = Dir1 & "\SubFolder2"
    ' Create folders...
    On Error Resume Next
    MkDir (Dir1)
    MkDir (Dir2)
    MkDir (Dir3)
    On Error GoTo 0
    End Sub[/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Const ROOT_FOLDER As String = "C:\" '"C:\Documents and Settings\Administrator\My Documents\Other\Test\"
    Dim LastRow As Long
    Dim Folder As String
    Dim i As Long
    Dim j As Long

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    On Error Resume Next
    For i = 1 To LastRow

    Folder = ROOT_FOLDER & .Cells(i, "A").Value
    MkDir Folder
    For j = 2 To .Cells(i, .Columns.Count).End(xlToLeft).Column

    Folder = Folder & Application.PathSeparator & .Cells(i, j).Value
    MkDir Folder
    Next j
    Next i
    On Error GoTo 0
    End With

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    May 2008
    Posts
    28
    Location
    In the first instance, I would suggest you take out the On Error Resume Next. Any errors caused by trying to create the directories will then be displayed to you.

Posting Permissions

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