Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 从电子表格数据创建文件夹层次结构_Excel_Vba_Directory_Hierarchy - Fatal编程技术网

Excel 从电子表格数据创建文件夹层次结构

Excel 从电子表格数据创建文件夹层次结构,excel,vba,directory,hierarchy,Excel,Vba,Directory,Hierarchy,我有几个数据从左到右组织的电子表格,我想从中创建文件夹。每一条记录都是完整的,没有空格,除非那是这一行的结尾,所以我要做的是: Col1 Col2 Col3 ------ ------ ------ Car Toyota Camry Car Toyota Corolla Truck Toyota Tacoma Car Toyota Yaris Car Ford Focus Car Ford

我有几个数据从左到右组织的电子表格,我想从中创建文件夹。每一条记录都是完整的,没有空格,除非那是这一行的结尾,所以我要做的是:

Col1     Col2     Col3
------   ------   ------
Car      Toyota   Camry
Car      Toyota   Corolla
Truck    Toyota   Tacoma
Car      Toyota   Yaris
Car      Ford     Focus
Car      Ford     Fusion
Truck    Ford     F150

Car
    Toyota
        Camry
        Corolla
        Yaris
    Ford
        Focus
        Fusion
Truck
    Toyota
        Tacoma
    Ford
        F-150
...
唯一需要注意的是,我有大约15列,其中一些条目在第3列或第4列结束,因此只需要创建那些文件夹

有人能帮我这个忙吗?我对编程并不陌生,但我对VBA还是相当陌生的


谢谢

试试这个。它假定您从列“A”开始,并且它还启动C:\(使用sDir变量)中的目录。如果需要,只需将“C:\”更改为您希望的基点

Option Explicit

Sub startCreating()
    Call CreateDirectory(2, 1)
End Sub

Sub CreateDirectory(ByVal row As Long, ByVal col As Long, Optional ByRef path As String)
    If (Len(ActiveSheet.Cells(row, col).Value) <= 0) Then
        Exit Sub
    End If

    Dim sDir As String

    If (Len(path) <= 0) Then
        path = ActiveSheet.Cells(row, col).Value
        sDir = "C:\" & path
    Else
        sDir = path & "\" & ActiveSheet.Cells(row, col).Value
    End If


    If (FileOrDirExists(sDir) = False) Then
        MkDir sDir
    End If

    If (Len(ActiveSheet.Cells(row, col + 1).Value) <= 0) Then
        Call CreateDirectory(row + 1, 1)
    Else
        Call CreateDirectory(row, col + 1, sDir)
    End If
End Sub


' Function thanks to: http://www.vbaexpress.com/kb/getarticle.php?kb_id=559
Function FileOrDirExists(PathName As String) As Boolean
     'Macro Purpose: Function returns TRUE if the specified file
     '               or folder exists, false if not.
     'PathName     : Supports Windows mapped drives or UNC
     '             : Supports Macintosh paths
     'File usage   : Provide full file path and extension
     'Folder usage : Provide full folder path
     '               Accepts with/without trailing "\" (Windows)
     '               Accepts with/without trailing ":" (Macintosh)

    Dim iTemp As Integer

     'Ignore errors to allow for error evaluation
    On Error Resume Next
    iTemp = GetAttr(PathName)

     'Check if error exists and set response appropriately
    Select Case Err.Number
    Case Is = 0
        FileOrDirExists = True
    Case Else
        FileOrDirExists = False
    End Select

     'Resume error checking
    On Error GoTo 0
End Function
选项显式
子startCreating()
调用CreateDirectory(2,1)
端接头
子CreateDirectory(ByVal行为长,ByVal列为长,可选ByRef路径为字符串)

If(Len(ActiveSheet.Cells(row,col).Value)尝试一下。它假设您从列“A”开始,并且它也从C:\(使用sDir变量)中的目录开始。如果需要,只需将“C:\”更改为您想要的基点

Option Explicit

Sub startCreating()
    Call CreateDirectory(2, 1)
End Sub

Sub CreateDirectory(ByVal row As Long, ByVal col As Long, Optional ByRef path As String)
    If (Len(ActiveSheet.Cells(row, col).Value) <= 0) Then
        Exit Sub
    End If

    Dim sDir As String

    If (Len(path) <= 0) Then
        path = ActiveSheet.Cells(row, col).Value
        sDir = "C:\" & path
    Else
        sDir = path & "\" & ActiveSheet.Cells(row, col).Value
    End If


    If (FileOrDirExists(sDir) = False) Then
        MkDir sDir
    End If

    If (Len(ActiveSheet.Cells(row, col + 1).Value) <= 0) Then
        Call CreateDirectory(row + 1, 1)
    Else
        Call CreateDirectory(row, col + 1, sDir)
    End If
End Sub


' Function thanks to: http://www.vbaexpress.com/kb/getarticle.php?kb_id=559
Function FileOrDirExists(PathName As String) As Boolean
     'Macro Purpose: Function returns TRUE if the specified file
     '               or folder exists, false if not.
     'PathName     : Supports Windows mapped drives or UNC
     '             : Supports Macintosh paths
     'File usage   : Provide full file path and extension
     'Folder usage : Provide full folder path
     '               Accepts with/without trailing "\" (Windows)
     '               Accepts with/without trailing ":" (Macintosh)

    Dim iTemp As Integer

     'Ignore errors to allow for error evaluation
    On Error Resume Next
    iTemp = GetAttr(PathName)

     'Check if error exists and set response appropriately
    Select Case Err.Number
    Case Is = 0
        FileOrDirExists = True
    Case Else
        FileOrDirExists = False
    End Select

     'Resume error checking
    On Error GoTo 0
End Function
选项显式
子startCreating()
调用CreateDirectory(2,1)
端接头
子CreateDirectory(ByVal行为长,ByVal列为长,可选ByRef路径为字符串)

If(Len(ActiveSheet.Cells(row,col.Value)我发现了一种做同样事情的更好方法,代码更少,效率更高如果文件夹名称中包含空格,则引用路径。如果需要,命令行mkdir将创建任何中间文件夹,以使整个路径存在。因此,您只需使用\作为分隔符来连接单元格,以指定路径,然后

如果Dir(YourPath,vbDirectory)=“”,那么
Shell(“cmd/c mkdir”“&YourPath&”“)
如果结束

我发现了一种更好的方法来做同样的事情,代码更少,效率更高如果文件夹名称中包含空格,则引用路径。如果需要,命令行mkdir将创建任何中间文件夹,以使整个路径存在。因此,您只需使用\作为分隔符来连接单元格,以指定路径,然后

如果Dir(YourPath,vbDirectory)=“”,那么
Shell(“cmd/c mkdir”“&YourPath&”“)
如果结束