Creating folders and sub-folders with a VBA macro












3















I'm looking to use a spreadsheet I've created to generate folders and sub-folders based on what is in each column.



The first column is the top level, the second column the next level down (sub-folder) and so on.



    A                 B                   C                   D
1 TOP FOLDER 1 Sub Folder 1.1 Sub Folder 1.2 Sub Folder 1.3
2 TOP FOLDER 2 Sub Folder 2.1 Sub Folder 2.2 Sub Folder 2.3
3 TOP FOLDER 3 Sub Folder 3.1 Sub Folder 3.2 Sub Folder 3.3


I've tried another program already and it made the folders but put them all in one folder! I need it with sub-folders, but I think an issue might be separating the folders, here's an example:



I'm thinking it may be problematic to separate the sub-folders into their parent folders because they are in the same columns...










share|improve this question





























    3















    I'm looking to use a spreadsheet I've created to generate folders and sub-folders based on what is in each column.



    The first column is the top level, the second column the next level down (sub-folder) and so on.



        A                 B                   C                   D
    1 TOP FOLDER 1 Sub Folder 1.1 Sub Folder 1.2 Sub Folder 1.3
    2 TOP FOLDER 2 Sub Folder 2.1 Sub Folder 2.2 Sub Folder 2.3
    3 TOP FOLDER 3 Sub Folder 3.1 Sub Folder 3.2 Sub Folder 3.3


    I've tried another program already and it made the folders but put them all in one folder! I need it with sub-folders, but I think an issue might be separating the folders, here's an example:



    I'm thinking it may be problematic to separate the sub-folders into their parent folders because they are in the same columns...










    share|improve this question



























      3












      3








      3


      2






      I'm looking to use a spreadsheet I've created to generate folders and sub-folders based on what is in each column.



      The first column is the top level, the second column the next level down (sub-folder) and so on.



          A                 B                   C                   D
      1 TOP FOLDER 1 Sub Folder 1.1 Sub Folder 1.2 Sub Folder 1.3
      2 TOP FOLDER 2 Sub Folder 2.1 Sub Folder 2.2 Sub Folder 2.3
      3 TOP FOLDER 3 Sub Folder 3.1 Sub Folder 3.2 Sub Folder 3.3


      I've tried another program already and it made the folders but put them all in one folder! I need it with sub-folders, but I think an issue might be separating the folders, here's an example:



      I'm thinking it may be problematic to separate the sub-folders into their parent folders because they are in the same columns...










      share|improve this question
















      I'm looking to use a spreadsheet I've created to generate folders and sub-folders based on what is in each column.



      The first column is the top level, the second column the next level down (sub-folder) and so on.



          A                 B                   C                   D
      1 TOP FOLDER 1 Sub Folder 1.1 Sub Folder 1.2 Sub Folder 1.3
      2 TOP FOLDER 2 Sub Folder 2.1 Sub Folder 2.2 Sub Folder 2.3
      3 TOP FOLDER 3 Sub Folder 3.1 Sub Folder 3.2 Sub Folder 3.3


      I've tried another program already and it made the folders but put them all in one folder! I need it with sub-folders, but I think an issue might be separating the folders, here's an example:



      I'm thinking it may be problematic to separate the sub-folders into their parent folders because they are in the same columns...







      microsoft-excel vba






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 19 '14 at 11:54









      nixda

      21.1k1178133




      21.1k1178133










      asked Aug 18 '14 at 19:51









      user3471595user3471595

      16112




      16112






















          3 Answers
          3






          active

          oldest

          votes


















          5














          This creates a folder structure with VBA. Short and beautiful.



          Sub CreateFolderStructure()
          Dim objRow as Range, objCell as Range, strFolders as String

          For Each objRow In ActiveSheet.UsedRange.Rows
          strFolders = "C:myRootFolder"
          For Each objCell In objRow.Cells
          strFolders = strFolders & "" & objCell
          Next
          Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
          Next
          End Sub


          No error handling!



          What it does




          1. Loop through every used row of your active Excel sheet

          2. Set the root folder in which our new folders should be created. Must be done in every loop

          3. Loop through every used cell in the current row

          4. Concatenate the root folder with a backslash and the new sub folder.

            Do this for every sub folder in that row until we get something like
            "C:myRootFolderTOP FOLDER 1SUB FOLDER 1.1SUB FOLDER 1.2SUB FOLDER 1.3"

          5. Now comes the magic. We do not use VBA's mkdir function.

            Instead we use Shell(cmd /c md) which can make several folders with one command. It also produces no error if a folder already exists. Such a beautiful command


          Some notes




          • Avoid these characters in folder names: © ® " - & ' ^ ( ) @

          • Empty Excel cells are no problem. The MD command can handle strings like C:root\subfolder with two consecutive backslashes

          • Spaces in folder names are no problem since we wrap the complete structure with two quotation marks (chr(34))






          share|improve this answer


























          • This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…

            – ThomasDoe
            Jul 5 '16 at 17:21



















          0














          Sub MkDirs()

          Const RootPath = "C:yourpath"
          Dim rng As Range

          Set rng = Selection

          For Each rw In rng.Rows
          ChDir RootPath
          For Each cl In rw.Cells
          If cl <> "" Then
          MkDir cl
          ChDir cl
          End If
          Next
          Next
          End Sub





          share|improve this answer

































            0














            Here is a better answer that allows you to choose a root file instead of defining it in the code:



            Sub FolderCreator()

            Dim objRow As Range, objCell As Range, strFolders As String, rootFolder As String

            With Application.FileDialog(msoFileDialogFolderPicker)
            ' show the file picker dialog box
            If .Show <> 0 Then
            rootFolder = .SelectedItems(1)
            End If
            End With

            For Each objRow In ActiveSheet.UsedRange.Rows
            strFolders = rootFolder
            For Each objCell In objRow.Cells
            strFolders = strFolders & "" & objCell
            Next
            Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
            Next

            End Sub





            share|improve this answer

























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f799666%2fcreating-folders-and-sub-folders-with-a-vba-macro%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              This creates a folder structure with VBA. Short and beautiful.



              Sub CreateFolderStructure()
              Dim objRow as Range, objCell as Range, strFolders as String

              For Each objRow In ActiveSheet.UsedRange.Rows
              strFolders = "C:myRootFolder"
              For Each objCell In objRow.Cells
              strFolders = strFolders & "" & objCell
              Next
              Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
              Next
              End Sub


              No error handling!



              What it does




              1. Loop through every used row of your active Excel sheet

              2. Set the root folder in which our new folders should be created. Must be done in every loop

              3. Loop through every used cell in the current row

              4. Concatenate the root folder with a backslash and the new sub folder.

                Do this for every sub folder in that row until we get something like
                "C:myRootFolderTOP FOLDER 1SUB FOLDER 1.1SUB FOLDER 1.2SUB FOLDER 1.3"

              5. Now comes the magic. We do not use VBA's mkdir function.

                Instead we use Shell(cmd /c md) which can make several folders with one command. It also produces no error if a folder already exists. Such a beautiful command


              Some notes




              • Avoid these characters in folder names: © ® " - & ' ^ ( ) @

              • Empty Excel cells are no problem. The MD command can handle strings like C:root\subfolder with two consecutive backslashes

              • Spaces in folder names are no problem since we wrap the complete structure with two quotation marks (chr(34))






              share|improve this answer


























              • This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…

                – ThomasDoe
                Jul 5 '16 at 17:21
















              5














              This creates a folder structure with VBA. Short and beautiful.



              Sub CreateFolderStructure()
              Dim objRow as Range, objCell as Range, strFolders as String

              For Each objRow In ActiveSheet.UsedRange.Rows
              strFolders = "C:myRootFolder"
              For Each objCell In objRow.Cells
              strFolders = strFolders & "" & objCell
              Next
              Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
              Next
              End Sub


              No error handling!



              What it does




              1. Loop through every used row of your active Excel sheet

              2. Set the root folder in which our new folders should be created. Must be done in every loop

              3. Loop through every used cell in the current row

              4. Concatenate the root folder with a backslash and the new sub folder.

                Do this for every sub folder in that row until we get something like
                "C:myRootFolderTOP FOLDER 1SUB FOLDER 1.1SUB FOLDER 1.2SUB FOLDER 1.3"

              5. Now comes the magic. We do not use VBA's mkdir function.

                Instead we use Shell(cmd /c md) which can make several folders with one command. It also produces no error if a folder already exists. Such a beautiful command


              Some notes




              • Avoid these characters in folder names: © ® " - & ' ^ ( ) @

              • Empty Excel cells are no problem. The MD command can handle strings like C:root\subfolder with two consecutive backslashes

              • Spaces in folder names are no problem since we wrap the complete structure with two quotation marks (chr(34))






              share|improve this answer


























              • This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…

                – ThomasDoe
                Jul 5 '16 at 17:21














              5












              5








              5







              This creates a folder structure with VBA. Short and beautiful.



              Sub CreateFolderStructure()
              Dim objRow as Range, objCell as Range, strFolders as String

              For Each objRow In ActiveSheet.UsedRange.Rows
              strFolders = "C:myRootFolder"
              For Each objCell In objRow.Cells
              strFolders = strFolders & "" & objCell
              Next
              Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
              Next
              End Sub


              No error handling!



              What it does




              1. Loop through every used row of your active Excel sheet

              2. Set the root folder in which our new folders should be created. Must be done in every loop

              3. Loop through every used cell in the current row

              4. Concatenate the root folder with a backslash and the new sub folder.

                Do this for every sub folder in that row until we get something like
                "C:myRootFolderTOP FOLDER 1SUB FOLDER 1.1SUB FOLDER 1.2SUB FOLDER 1.3"

              5. Now comes the magic. We do not use VBA's mkdir function.

                Instead we use Shell(cmd /c md) which can make several folders with one command. It also produces no error if a folder already exists. Such a beautiful command


              Some notes




              • Avoid these characters in folder names: © ® " - & ' ^ ( ) @

              • Empty Excel cells are no problem. The MD command can handle strings like C:root\subfolder with two consecutive backslashes

              • Spaces in folder names are no problem since we wrap the complete structure with two quotation marks (chr(34))






              share|improve this answer















              This creates a folder structure with VBA. Short and beautiful.



              Sub CreateFolderStructure()
              Dim objRow as Range, objCell as Range, strFolders as String

              For Each objRow In ActiveSheet.UsedRange.Rows
              strFolders = "C:myRootFolder"
              For Each objCell In objRow.Cells
              strFolders = strFolders & "" & objCell
              Next
              Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
              Next
              End Sub


              No error handling!



              What it does




              1. Loop through every used row of your active Excel sheet

              2. Set the root folder in which our new folders should be created. Must be done in every loop

              3. Loop through every used cell in the current row

              4. Concatenate the root folder with a backslash and the new sub folder.

                Do this for every sub folder in that row until we get something like
                "C:myRootFolderTOP FOLDER 1SUB FOLDER 1.1SUB FOLDER 1.2SUB FOLDER 1.3"

              5. Now comes the magic. We do not use VBA's mkdir function.

                Instead we use Shell(cmd /c md) which can make several folders with one command. It also produces no error if a folder already exists. Such a beautiful command


              Some notes




              • Avoid these characters in folder names: © ® " - & ' ^ ( ) @

              • Empty Excel cells are no problem. The MD command can handle strings like C:root\subfolder with two consecutive backslashes

              • Spaces in folder names are no problem since we wrap the complete structure with two quotation marks (chr(34))







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 5 '16 at 17:37

























              answered Aug 19 '14 at 11:48









              nixdanixda

              21.1k1178133




              21.1k1178133













              • This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…

                – ThomasDoe
                Jul 5 '16 at 17:21



















              • This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…

                – ThomasDoe
                Jul 5 '16 at 17:21

















              This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…

              – ThomasDoe
              Jul 5 '16 at 17:21





              This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…

              – ThomasDoe
              Jul 5 '16 at 17:21













              0














              Sub MkDirs()

              Const RootPath = "C:yourpath"
              Dim rng As Range

              Set rng = Selection

              For Each rw In rng.Rows
              ChDir RootPath
              For Each cl In rw.Cells
              If cl <> "" Then
              MkDir cl
              ChDir cl
              End If
              Next
              Next
              End Sub





              share|improve this answer






























                0














                Sub MkDirs()

                Const RootPath = "C:yourpath"
                Dim rng As Range

                Set rng = Selection

                For Each rw In rng.Rows
                ChDir RootPath
                For Each cl In rw.Cells
                If cl <> "" Then
                MkDir cl
                ChDir cl
                End If
                Next
                Next
                End Sub





                share|improve this answer




























                  0












                  0








                  0







                  Sub MkDirs()

                  Const RootPath = "C:yourpath"
                  Dim rng As Range

                  Set rng = Selection

                  For Each rw In rng.Rows
                  ChDir RootPath
                  For Each cl In rw.Cells
                  If cl <> "" Then
                  MkDir cl
                  ChDir cl
                  End If
                  Next
                  Next
                  End Sub





                  share|improve this answer















                  Sub MkDirs()

                  Const RootPath = "C:yourpath"
                  Dim rng As Range

                  Set rng = Selection

                  For Each rw In rng.Rows
                  ChDir RootPath
                  For Each cl In rw.Cells
                  If cl <> "" Then
                  MkDir cl
                  ChDir cl
                  End If
                  Next
                  Next
                  End Sub






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 5 '15 at 19:48









                  Community

                  1




                  1










                  answered Aug 19 '14 at 12:03









                  MerzavetsMerzavets

                  1646




                  1646























                      0














                      Here is a better answer that allows you to choose a root file instead of defining it in the code:



                      Sub FolderCreator()

                      Dim objRow As Range, objCell As Range, strFolders As String, rootFolder As String

                      With Application.FileDialog(msoFileDialogFolderPicker)
                      ' show the file picker dialog box
                      If .Show <> 0 Then
                      rootFolder = .SelectedItems(1)
                      End If
                      End With

                      For Each objRow In ActiveSheet.UsedRange.Rows
                      strFolders = rootFolder
                      For Each objCell In objRow.Cells
                      strFolders = strFolders & "" & objCell
                      Next
                      Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
                      Next

                      End Sub





                      share|improve this answer






























                        0














                        Here is a better answer that allows you to choose a root file instead of defining it in the code:



                        Sub FolderCreator()

                        Dim objRow As Range, objCell As Range, strFolders As String, rootFolder As String

                        With Application.FileDialog(msoFileDialogFolderPicker)
                        ' show the file picker dialog box
                        If .Show <> 0 Then
                        rootFolder = .SelectedItems(1)
                        End If
                        End With

                        For Each objRow In ActiveSheet.UsedRange.Rows
                        strFolders = rootFolder
                        For Each objCell In objRow.Cells
                        strFolders = strFolders & "" & objCell
                        Next
                        Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
                        Next

                        End Sub





                        share|improve this answer




























                          0












                          0








                          0







                          Here is a better answer that allows you to choose a root file instead of defining it in the code:



                          Sub FolderCreator()

                          Dim objRow As Range, objCell As Range, strFolders As String, rootFolder As String

                          With Application.FileDialog(msoFileDialogFolderPicker)
                          ' show the file picker dialog box
                          If .Show <> 0 Then
                          rootFolder = .SelectedItems(1)
                          End If
                          End With

                          For Each objRow In ActiveSheet.UsedRange.Rows
                          strFolders = rootFolder
                          For Each objCell In objRow.Cells
                          strFolders = strFolders & "" & objCell
                          Next
                          Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
                          Next

                          End Sub





                          share|improve this answer















                          Here is a better answer that allows you to choose a root file instead of defining it in the code:



                          Sub FolderCreator()

                          Dim objRow As Range, objCell As Range, strFolders As String, rootFolder As String

                          With Application.FileDialog(msoFileDialogFolderPicker)
                          ' show the file picker dialog box
                          If .Show <> 0 Then
                          rootFolder = .SelectedItems(1)
                          End If
                          End With

                          For Each objRow In ActiveSheet.UsedRange.Rows
                          strFolders = rootFolder
                          For Each objCell In objRow.Cells
                          strFolders = strFolders & "" & objCell
                          Next
                          Shell ("cmd /c md " & Chr(34) & strFolders & Chr(34))
                          Next

                          End Sub






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 23 at 23:23









                          Scott

                          15.9k113990




                          15.9k113990










                          answered Jan 23 at 22:47









                          Quinn NerenbergQuinn Nerenberg

                          1




                          1






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Super User!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f799666%2fcreating-folders-and-sub-folders-with-a-vba-macro%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Plaza Victoria

                              In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

                              How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...