跳到主要内容

从 Excel 中的数据列表创建多个文件夹和子文件夹

假设您有一个工作表范围内的员工姓名列表,并且旨在为每个人创建单独的文件夹来存储他们的信息。手动创建每个文件夹可能非常耗时。然而,有一些有效的方法可以加快这一过程。在本指南中,我将分享几种根据指定单元格值快速生成文件夹的方法。

根据单元格值创建文件夹

使用 VBA 代码根据单元格值创建文件夹和子文件夹


根据单元格值创建文件夹

在本节中,我们将详细探讨各种方法,提供全面的分步说明,以根据单元格值列表快速轻松地创建文件夹。

使用 MD 命令和记事本从列表创建文件夹

使用 MD 命令和记事本将 Excel 中的列表转换为文件夹是一个聪明的技巧,它将简单的批处理脚本与 Excel 保持事物井然有序的技巧结合在一起。这种方法非常适合快速制作大量文件夹,而无需全部手工完成。以下是完成此任务的分步指南:

第一步:使用MD命令创建公式

将以下公式复制或输入到第一个单元格值(例如 B1)旁边的空白单元格中,然后向下拖动填充柄以将公式应用到所有列表项。

="MD "&A1

步骤 2:将公式复制并粘贴到记事本文件中

  1. 媒体 按Ctrl + C 使用 MD 命令公式复制单元格。
  2. 可选 记事本 并按下 按Ctrl + V 将命令粘贴到新文件中。

步骤 3:将记事本文件另存为 .bat 文件

点击 除上文所 来自 文件 记事本中的选项卡中 除上文所 对话框中,选择要在其中创建多个文件夹的目录,然后用 。BAT 扩大。最后,点击 保存 按钮。 看截图:

第四步:双击.bat文件生成多个文件夹

  1. 关闭记事本文件,导航到之前保存 .bat 文件的文件夹。
  2. 现在,见证奇迹:双击该文件,您将看到同时创建多个文件夹。请参阅下面的演示:
 

使用强大的工具从列表创建文件夹 - Kutools for Excel

随着强大 Kutools for Excel从单元格内容创建文件夹 功能,您现在可以轻松快速地从 Excel 列表创建文件夹。但它并不仅仅停留在基本文件夹; Kutools 还允许您一次性创建具有多层子文件夹的复杂结构。只需几个简单的步骤即可将 Excel 中的数据转换为组织有序的文件夹系统,从而显着提高您的工作效率。

备注: 如果你想用这个 从单元格内容创建文件夹 功能,请 下载并安装 Kutools for Excel 第一。

安装后 Kutools for Excel请点击 Kutools 加 > 导入/导出 > 从单元格内容创建文件夹 打开 从单元格内容创建文件夹 对话框:

  1. 选择要基于其创建文件夹的单元格值;
  2. 然后,点击 按钮指定要保存文件夹的目标文件夹;
  3. 最后点击 OK 按钮。

结果:

Kutools 将处理您工作表中的列表,并为指定目的地中的每个条目创建一个文件夹。导航到目标文件夹以查看结果。看截图:

提示:
  1. 这个有用的功能还可以帮助 创建文件夹及其子文件夹 根据您的需要。为此,您应该在单元格中输入所需的文件夹和子文件夹名称,并使用反斜杠符号 (\) 分隔每个级别。每个单元格的内容将作为设置文件夹和子文件夹所需结构的指南。

    然后,应用 从单元格内容创建文件夹 功能,所有文件夹及其子文件夹都将成功创建。看截图:
  2. 要应用此功能,请 下载并安装 Kutools for Excel 第一。
 

使用 VBA 代码从列表创建文件夹

在 Excel 中使用 VBA 代码可以将从列表中创建文件夹的繁琐任务转变为快速、自动化的过程。本节将向您展示如何应用 VBA 代码来生成文件夹。

第 1 步:打开 VBA 模块编辑器并复制代码

  1. 按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。
  2. 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。
    VBA 代码:根据单元格值列表创建文件夹
    Sub CreateFoldersFromSelection()
    'Updateby Extendoffice
        Dim FolderPath As String
        Dim Cell As Range
        Dim SelectedRange As Range
        Dim FolderName As String
        On Error Resume Next
        Set SelectedRange = Application.InputBox("Select the range with folder names", "Kutools for Excel", Type:=8)
        If SelectedRange Is Nothing Then Exit Sub
        On Error GoTo 0
        
        With Application.FileDialog(msoFileDialogFolderPicker)
            .Title = "Select the destination Folder"
            .AllowMultiSelect = False
            If .Show <> -1 Then Exit Sub
            FolderPath = .SelectedItems(1) & "\"
        End With
        
        For Each Cell In SelectedRange
            FolderName = FolderPath & Cell.Value
            If Cell.Value <> "" And Not FolderExists(FolderName) Then
                MkDir FolderName
            End If
        Next Cell
    End Sub
    
    Function FolderExists(ByVal Path As String) As Boolean
        On Error Resume Next
        FolderExists = (GetAttr(Path) And vbDirectory) = vbDirectory
        On Error GoTo 0
    End Function
    

第2步:执行代码

  1. 粘贴此代码后,请按 F5 键来运行此代码。在提示框中,选择要从中创建文件夹的单元格值。然后,单击 OK.
  2. 那么,在下面的 选择目标文件夹 窗口中,指定输出创建的文件夹的目标路径。然后,单击 OK 按钮,请参见屏幕截图:

结果:

执行 VBA 代码后,前往目标目录查看结果。在那里,您将找到新创建的文件夹,每个文件夹都对应于 Excel 列表中的一个项目。看截图:

提示:
  1. 如果单元格中有重复的条目,运行代码将导致只为这些重复项创建一个文件夹。
  2. 如果您发现自己经常使用此代码,请考虑将工作簿保存在 Excel 启用宏的工作簿 格式。此操作会保留工作簿中的代码,使您将来可以直接执行它,而无需重新输入或重新导入代码。

使用 VBA 代码根据单元格值创建文件夹和子文件夹

有时,您可能会发现自己不仅需要生成文件夹,还需要生成其相应的子文件夹,所有这些都基于 Excel 单元格中的数据。为了实现这个任务,在这里,我将介绍一段VBA代码。

第 1 步:准备数据

首先,您应该输入如下屏幕截图所示的数据,将主文件夹名称放在第一列中,将子文件夹的名称放在第二列中。

第 2 步:打开 VBA 模块编辑器并复制代码

  1. 按住 ALT + F11 键,然后打开 Microsoft Visual Basic应用程序 窗口。
  2. 点击 插页 > 模块,然后将以下代码粘贴到 模块 窗口。
    VBA 代码:基于单元格值创建文件夹和子文件夹
    Sub CreateFoldersAndSubfoldersWithUserInput()
    'Updateby Extendoffice
        Dim Rng As Range
        Dim Cell As Range
        Dim basePath As String
        Dim fldrPicker As FileDialog
        Dim FolderPath As String, subfolderPath As String
        On Error Resume Next
        Set Rng = Application.InputBox("Select the range of cells (two columns: one is folder column, another s subfolder column):", "Kutools for Excel", Type:=8)
        If Rng Is Nothing Then Exit Sub
        On Error GoTo 0
        Set fldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
        With fldrPicker
            .Title = "Select the Base Folder Path"
            .AllowMultiSelect = False
            If .Show <> -1 Then Exit Sub
            basePath = .SelectedItems(1)
        End With
        If Right(basePath, 1) <> "\" Then basePath = basePath & "\"
        For Each Cell In Rng.Columns(1).Cells
            If Not Cell.Value = "" Then
                FolderPath = basePath & Cell.Value
                If Not FolderExists(FolderPath) Then MkDir FolderPath
                If Not Cell.Offset(0, 1).Value = "" Then
                    subfolderPath = FolderPath & "\" & Cell.Offset(0, 1).Value
                    If Not FolderExists(subfolderPath) Then MkDir subfolderPath
                End If
            End If
        Next Cell
    End Sub
    
    Function FolderExists(FolderPath As String) As Boolean
        On Error Resume Next
        FolderExists = (GetAttr(FolderPath) And vbDirectory) = vbDirectory
        On Error GoTo 0
    End Function
    

第3步:执行代码

  1. 粘贴此代码后,请按 F5 键来运行此代码。在提示框中,选择要从中创建文件夹的单元格值。然后,单击 OK.
  2. 在下面弹出的窗口中,指定输出创建的文件夹的目标路径。然后,单击 OK 按钮,请参见屏幕截图:

结果:

执行VBA代码后,进入目标目录查看结果。您会发现文件夹及其各自的子文件夹(由单元格值指示)已成功创建,如下图所示:

提示:
  1. 此代码仅可用于创建主文件夹及其第一级子文件夹。
  2. 如果您发现自己经常使用此代码,请考虑将工作簿保存在 Excel 启用宏的工作簿 格式。此操作会保留工作簿中的代码,使您将来可以直接执行它,而无需重新输入或重新导入代码。

相关文章:

  • 列出Excel中的所有文件夹和子文件夹
  • 您是否曾经遇到过将指定目录中的所有文件夹和子文件夹列出到工作表中的问题? 在Excel中,没有一种快速简便的方法可以一次获取特定目录中所有文件夹的名称。 为了处理该任务,本文可能会对您有所帮助。
  • 根据列表将文件从一个文件夹复制或移动到另一个文件夹
  • 如果您在工作表的一列中有文件名列表,并且这些文件位于您的Computor的文件夹中。 但是,现在,您需要将这些名称已在工作表中列出的文件从其原始文件夹移动或复制到另一个文件,如下图所示。 您如何在Excel中尽快完成此任务?
  • 重命名文件夹中的多个文件
  • 可能我们大多数人都遭受这个问题的困扰,我们需要在一个文件夹中重命名多个文件,如果该文件夹中有成百上千个文件,那么一个文件名一个一个地重命名会使我们发疯。 我们有什么好的职能来处理这项任务吗?
Comments (63)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
níže CZ verze

EN:

A better way to do this in a few seconds is to use cmd (.bat file)

If you have a list of names in excel, add the word MKdir in front of the name (folder name) and if it contains a space put the name in quotes. Then just copy it to notepad, save as and add the .bat extension. Once you have this, substitute the .bat file in the folder where it wants to be created and you're done.

If you want the cmd not to close write at the end of the puase like below

Here is the 3 word code *5* :

start
________
MKdir "Pixie Pin"

pause
________
end


this creates a folder named Pixie Pin in the folder where the command was run

CZ:

Lepší způsob jak to udělat během par sec. je použít cmd (.bat soubor)

Pokud máte seznam jmen v excelu, doplňte pomocí vzorečku slovo MKdir před jmeno (název složky) a pokud obsahuje mezeru dejte název do uvozovek. Poté stačí jen zkopírovat do oznámkového bloku (NotePad), dát uložit jako a dopsat příponu .bat . Jakmile toto máte, supsťte .bat soubor ve složce kde chce aby se vytvořili a máte to.

Pokud chcete aby se cmd nezavřelo napište na konec puase jako je níže

Zde je ten 3 slovný kód *5* :

start
________
MKdir "Pixie Pin"

pause
________
konec


toto vytvoří složku s názvem Pixie Pin ve složce kde byl příkaz spuštěn
This comment was minimized by the moderator on the site
This worked really well, even for someone with zero experience with VBA :-)
Would it be possible to adapt the macro or extend the macro to also create hyperlinks to the folders in the selected cells?
So for instance, Cell A3 is selected and you run the macro and the folder is created. Would it be possible to make cell A3 a hyperlink to the folder by expanding on the macro instead of doing that manually?
This comment was minimized by the moderator on the site
Hello, Marloes
To create hyperlinks for the cell values, the following vba code may help you:

First, please select the cell values, and then run this code, and select a folder for outputting the folders.

Sub MakeFoldersAndAddHyperlinksWithFolderSelection()
    Dim Rng As Range
    Dim maxRows, maxCols, r, c As Integer
    Dim folderPath As String
    Dim baseFolderPath As String
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    
    With fd
        If .Show = -1 Then
            baseFolderPath = .SelectedItems(1) & "\"
        Else
            MsgBox "No folder selected. Operation Cancelled."
            Exit Sub
        End If
    End With
    
    Set Rng = Selection
    maxRows = Rng.Rows.Count
    maxCols = Rng.Columns.Count
    
    For c = 1 To maxCols
        For r = 1 To maxRows
            folderPath = baseFolderPath & Rng.Cells(r, c).Value
            If Len(Dir(folderPath, vbDirectory)) = 0 Then
                MkDir folderPath
                On Error Resume Next
                ActiveSheet.Hyperlinks.Add Anchor:=Rng.Cells(r, c), Address:=folderPath, TextToDisplay:=Rng.Cells(r, c).Value
                On Error GoTo 0
            End If
        Next r
    Next c
End Sub


Please have a try, thank you!
This comment was minimized by the moderator on the site
please, i need that same macro but instead of saving them as folders, i need it to save as Excels.
This comment was minimized by the moderator on the site
is it possible to introduce a condition where if that condition is met the module can create 2 folders (each using a different path)?
if the first list of folders is in the A column then the condition occurs in the U column. The conditional criteria is whether the cell is empty or not.
if the condition is not met the module only makes one folder based on the selection.
This comment was minimized by the moderator on the site
Hi, a_c, sorry I have not found a method can solve this job yet.
This comment was minimized by the moderator on the site
Thank you very much
This comment was minimized by the moderator on the site
Thanks a lot! Your VBA code is really super
This comment was minimized by the moderator on the site
Is it possible to import data from a word to excel on colors algorythme? So, I spell the cities with red and countries with blue in a word, and the to import only these to excel. I don’t know if I made myself clear. Thanks
This comment was minimized by the moderator on the site
Thank you, this has saved me literally days of work.
This comment was minimized by the moderator on the site
Hello,


For the following code it shows error in

MkDir (ActiveWorkbook.Path & "\" & Rng(r, c))



It says Runtime error 76 path not found



Can someone please help me with this?

There are no unsupported characters in the file path.
Not sure what could be the problem

Thanks for the help!
This comment was minimized by the moderator on the site
thank you , time saved
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations