跳到主要內容

從 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

與強大的 Excel的Kutools從單元格內容創建文件夾 功能,您現在可以輕鬆快速地從 Excel 清單建立資料夾。但它並不僅僅停留在基本資料夾; Kutools 還允許您一次建立具有多層子資料夾的複雜結構。只需幾個簡單的步驟即可將 Excel 中的資料轉換為組織有序的資料夾系統,從而顯著提高您的工作效率。

備註: 如果你想用這個 從單元格內容創建文件夾 功能,請 下載並安裝 Kutools for Excel 第一。

安裝後 Excel的Kutools請點擊 Kutools 加 > 導入/導出 > 從單元格內容創建文件夾 打開 從單元格內容創建文件夾 對話框:

  1. 選擇要基於其建立資料夾的儲存格值;
  2. 然後,點擊 按鈕指定要儲存資料夾的目標資料夾;
  3. 最後點擊 OK 按鈕。

結果:

Kutools 將處理您工作表中的列表,並為指定目的地中的每個條目建立一個資料夾。導航到目標資料夾以查看結果。看截圖:

提示:
  1. 這個有用的功能還可以幫助 建立資料夾及其子資料夾 根據您的需要。為此,您應該在儲存格中輸入所需的資料夾和子資料夾名稱,並使用反斜線符號 (\) 分隔每個層級。每個單元格的內容將作為設定資料夾和子資料夾所需結構的指南。

    然後,應用 從單元格內容創建文件夾 功能,所有資料夾及其子資料夾都將成功建立。看截圖:
  2. 要應用此功能,請 下載並安裝 Kutools for Excel 第一。
 

使用 VBA 程式碼從清單建立資料夾

在 Excel 中使用 VBA 程式碼可以將從清單中建立資料夾的繁瑣任務轉變為快速、自動化的流程。本節將向您展示如何套用 VBA 程式碼來產生資料夾。

第 1 步:打開 VBA 模塊編輯器並複制代碼

  1. 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications 窗口。
  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 for Applications 窗口。
  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