我在excel中有两列数据,我试图将它们转换为文件夹和子文件夹的列表。 A列将是主文件夹的第一个列表,B列的每个条目将是A列对应文件夹中的子文件夹。最终结果将是20个文件夹,每个文件夹中都包含一个文件夹。我以前使用过此代码-
Sub MakeFolders()
Dim Rng As Range
Dim maxRows, maxCols, r, c As Integer
Set Rng = Selection
maxRows = Rng.Rows.Count
maxCols = Rng.Columns.Count
For c = 1 To maxCols
r = 1
Do While r <= maxRows
If Len(Dir(ActiveWorkbook.Path & "\" & Rng(r, c), vbDirectory)) = 0 Then
MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))
On Error Resume Next
End If
r = r + 1
Loop
Next c
End Sub
-从单个数据列创建单个文件夹的列表。我想知道如何更改该代码以在第一列中创建一个文件夹列表,并使第二列中的每个条目成为A列中相应文件夹内的子文件夹。Excel电子表格如下所示:>
Column A Column B
1 Folder 1 Subfolder in Folder 1
2 Folder 2 Subfolder in Folder 2
3 Folder 3 Subfolder in Folder 3
4 Folder 4 Subfolder in Folder 4
5 Folder 5 Subfolder in Folder 5
6 Folder 6 Subfolder in Folder 6
7 Folder 7 Subfolder in Folder 7
8 Folder 8 Subfolder in Folder 8
9 Folder 9 Subfolder in Folder 9
10 Folder 10 Subfolder in Folder 10
由于我对VBA的了解非常有限,因此不胜感激!
答案 0 :(得分:1)
未经测试:
Sub MakeFolders()
Dim Rng As Range, rw As Range, c As Range
Dim p As String, v As String
Set Rng = Selection
'process each selected row
For Each rw In Rng.Rows
p = ActiveWorkbook.Path & "\" 'set initial root path for this row
'process each cell in this row
For Each c In rw.Cells
v = Trim(c.Value) 'what's in the cell?
If Len(v) > 0 Then
If Len(Dir(p & v, vbDirectory)) = 0 Then MkDir (p & v) 'create if not already there
p = p & v & "\" 'append to path (regardless of whether it needed to be created)
End If
Next c
Next rw
End Sub