Creating folders and sub-folders with a VBA macro
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
add a comment |
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
add a comment |
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
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
microsoft-excel vba
edited Aug 19 '14 at 11:54
nixda
21.1k1178133
21.1k1178133
asked Aug 18 '14 at 19:51
user3471595user3471595
16112
16112
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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
- Loop through every used row of your active Excel sheet
- Set the root folder in which our new folders should be created. Must be done in every loop
- Loop through every used cell in the current row
- 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"
- Now comes the magic. We do not use VBA's
mkdir
function.
Instead we useShell(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)
)
This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…
– ThomasDoe
Jul 5 '16 at 17:21
add a comment |
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
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
- Loop through every used row of your active Excel sheet
- Set the root folder in which our new folders should be created. Must be done in every loop
- Loop through every used cell in the current row
- 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"
- Now comes the magic. We do not use VBA's
mkdir
function.
Instead we useShell(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)
)
This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…
– ThomasDoe
Jul 5 '16 at 17:21
add a comment |
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
- Loop through every used row of your active Excel sheet
- Set the root folder in which our new folders should be created. Must be done in every loop
- Loop through every used cell in the current row
- 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"
- Now comes the magic. We do not use VBA's
mkdir
function.
Instead we useShell(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)
)
This code returns a Runtime error 424. It is fixed at mrexcel.com/forum/excel-questions/…
– ThomasDoe
Jul 5 '16 at 17:21
add a comment |
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
- Loop through every used row of your active Excel sheet
- Set the root folder in which our new folders should be created. Must be done in every loop
- Loop through every used cell in the current row
- 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"
- Now comes the magic. We do not use VBA's
mkdir
function.
Instead we useShell(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)
)
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
- Loop through every used row of your active Excel sheet
- Set the root folder in which our new folders should be created. Must be done in every loop
- Loop through every used cell in the current row
- 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"
- Now comes the magic. We do not use VBA's
mkdir
function.
Instead we useShell(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)
)
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Aug 5 '15 at 19:48
Community♦
1
1
answered Aug 19 '14 at 12:03
MerzavetsMerzavets
1646
1646
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Jan 23 at 23:23
Scott
15.9k113990
15.9k113990
answered Jan 23 at 22:47
Quinn NerenbergQuinn Nerenberg
1
1
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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