ไปยังเนื้อหาหลัก

สร้างหลายโฟลเดอร์และโฟลเดอร์ย่อยจากรายการข้อมูลใน Excel

สมมติว่าคุณมีรายชื่อพนักงานภายในช่วงเวิร์กชีทและมีเป้าหมายที่จะสร้างโฟลเดอร์แต่ละโฟลเดอร์สำหรับแต่ละคนเพื่อจัดเก็บข้อมูล การสร้างแต่ละโฟลเดอร์ด้วยตนเองอาจใช้เวลานาน อย่างไรก็ตาม มีวิธีการที่มีประสิทธิภาพในการเร่งกระบวนการนี้ ในคู่มือนี้ ฉันจะแชร์วิธีการต่างๆ มากมายในการสร้างโฟลเดอร์อย่างรวดเร็วตามค่าของเซลล์ที่ระบุ

สร้างโฟลเดอร์ตามค่าของเซลล์

สร้างโฟลเดอร์และโฟลเดอร์ย่อยตามค่าเซลล์ด้วยรหัส VBA


สร้างโฟลเดอร์ตามค่าของเซลล์

ในส่วนนี้ เราจะสำรวจวิธีการต่างๆ โดยละเอียด โดยให้คำแนะนำทีละขั้นตอนที่ครอบคลุมเพื่อสร้างโฟลเดอร์ตามรายการค่าของเซลล์อย่างรวดเร็วและง่ายดาย

สร้างโฟลเดอร์จากรายการโดยใช้คำสั่ง MD และ Notepad

การเปลี่ยนรายการจาก Excel ให้เป็นโฟลเดอร์โดยใช้คำสั่ง MD และ Notepad เป็นเคล็ดลับอัจฉริยะที่ผสมผสานการเขียนสคริปต์แบบกลุ่มง่ายๆ เข้ากับความสามารถของ Excel ในการจัดระเบียบสิ่งต่างๆ วิธีนี้เหมาะสำหรับสร้างโฟลเดอร์จำนวนมากอย่างรวดเร็ว โดยไม่ต้องทำด้วยมือทั้งหมด คำแนะนำทีละขั้นตอนเพื่อให้งานนี้สำเร็จ:

ขั้นตอนที่ 1: ใช้คำสั่ง MD เพื่อสร้างสูตร

คัดลอกหรือใส่สูตรต่อไปนี้ลงในเซลล์ว่างถัดจากค่าเซลล์แรกของคุณ (เช่น B1) จากนั้นลากจุดจับเติมลงเพื่อใช้สูตรกับรายการทั้งหมดของคุณ

="MD "&A1

ขั้นตอนที่ 2: คัดลอกและวางสูตรลงในไฟล์ Notepad

  1. ข่าวประชา Ctrl + C เพื่อคัดลอกเซลล์ด้วยสูตรคำสั่ง MD
  2. จุดเปิด Notepad และกด Ctrl + V เพื่อวางคำสั่งลงในไฟล์ใหม่

ขั้นตอนที่ 3: บันทึกไฟล์ Notepad เป็นไฟล์ .bat

คลิก บันทึกเป็น จาก เนื้อไม่มีมัน แท็บใน Notepad ในไฟล์ บันทึกเป็น กล่องโต้ตอบ เลือกไดเร็กทอรีที่คุณต้องการสร้างหลายโฟลเดอร์ จากนั้นตั้งชื่อไฟล์นี้ด้วย a . ค้างคาว ส่วนขยาย. สุดท้ายคลิก ลด ปุ่ม. ดูภาพหน้าจอ:

ขั้นตอนที่ 4: ดับเบิลคลิกไฟล์ .bat เพื่อสร้างหลายโฟลเดอร์

  1. ปิดไฟล์ Notepad นำทางไปยังโฟลเดอร์ที่คุณบันทึกไฟล์ .bat ไว้ก่อนหน้านี้
  2. ตอนนี้ มาเป็นสักขีพยานในความมหัศจรรย์: ดับเบิลคลิกที่ไฟล์ แล้วคุณจะเห็นหลายโฟลเดอร์ถูกสร้างขึ้นพร้อมกัน ดูการสาธิตด้านล่าง:
 

สร้างโฟลเดอร์จากรายการโดยใช้เครื่องมืออันทรงพลัง - Kutools for Excel

ด้วยพลังอันทรงพลัง Kutools สำหรับ Excel's สร้างโฟลเดอร์จากสารบัญเซลล์ คุณสามารถสร้างโฟลเดอร์จากรายการ Excel ได้อย่างง่ายดายและรวดเร็ว แต่มันไม่ได้หยุดอยู่แค่โฟลเดอร์พื้นฐานเท่านั้น Kutools ยังช่วยให้คุณสร้างโครงสร้างที่ซับซ้อนด้วยโฟลเดอร์ย่อยหลายระดับได้ในคราวเดียว เพียงไม่กี่ขั้นตอนง่ายๆ ก็สามารถแปลงข้อมูลจาก Excel ให้เป็นระบบโฟลเดอร์ที่จัดระเบียบได้ ซึ่งช่วยเพิ่มประสิทธิภาพการทำงานของคุณได้อย่างมาก

หมายเหตุ: หากคุณต้องการใช้สิ่งนี้ สร้างโฟลเดอร์จากสารบัญเซลล์ คุณสมบัติโปรด ดาวน์โหลดและติดตั้ง Kutools สำหรับ Excel ก่อน

หลังจากการติดตั้ง Kutools สำหรับ Excelกรุณาคลิกที่ Kutools พลัส > นำเข้าส่งออก > สร้างโฟลเดอร์จากสารบัญเซลล์ เพื่อเปิด สร้างโฟลเดอร์จากสารบัญเซลล์ กล่องโต้ตอบ:

  1. เลือกค่าเซลล์ที่คุณต้องการสร้างโฟลเดอร์ตาม
  2. จากนั้นให้คลิกที่ ปุ่มเพื่อระบุโฟลเดอร์ปลายทางที่คุณต้องการบันทึกโฟลเดอร์
  3. สุดท้ายคลิก OK ปุ่ม

ผลลัพธ์:

Kutools จะประมวลผลรายการจากแผ่นงานของคุณและสร้างโฟลเดอร์สำหรับแต่ละรายการในปลายทางที่ระบุ นำทางไปยังโฟลเดอร์ปลายทางเพื่อดูผลลัพธ์ ดูภาพหน้าจอ:

ทิปส์:
  1. คุณสมบัติที่มีประโยชน์นี้ยังสามารถช่วยได้ สร้างโฟลเดอร์พร้อมกับโฟลเดอร์ย่อย ตามที่คุณต้องการ ในการดำเนินการนี้ คุณควรป้อนโฟลเดอร์และชื่อโฟลเดอร์ย่อยที่ต้องการลงในเซลล์ โดยใช้เครื่องหมายแบ็กสแลช (\) เพื่อแยกแต่ละระดับ เนื้อหาของแต่ละเซลล์จะทำหน้าที่เป็นแนวทางในการตั้งค่าโครงสร้างโฟลเดอร์และโฟลเดอร์ย่อยที่ต้องการ

    จากนั้นจึงทา สร้างโฟลเดอร์จากสารบัญเซลล์ โฟลเดอร์ทั้งหมดพร้อมกับโฟลเดอร์ย่อยจะถูกสร้างขึ้นได้สำเร็จ ดูภาพหน้าจอ:
  2. หากต้องการใช้คุณลักษณะนี้ โปรด ดาวน์โหลดและติดตั้ง Kutools สำหรับ Excel ก่อน
 

สร้างโฟลเดอร์จากรายการโดยใช้โค้ด VBA

การใช้โค้ด VBA ใน Excel สามารถเปลี่ยนงานที่น่าเบื่อในการสร้างโฟลเดอร์จากรายการให้เป็นกระบวนการอัตโนมัติที่รวดเร็ว ส่วนนี้จะแสดงวิธีการใช้โค้ด VBA เพื่อสร้างโฟลเดอร์

ขั้นตอนที่ 1: เปิดตัวแก้ไขโมดูล VBA และคัดลอกโค้ด

  1. ค้างไว้ ALT + F11 ใน Excel และจะเปิดไฟล์ 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 ใน Excel และจะเปิดไฟล์ 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 ไม่มีวิธีที่รวดเร็วและสะดวกในการเรียกชื่อโฟลเดอร์ทั้งหมดในไดเร็กทอรีเฉพาะพร้อมกัน เพื่อจัดการกับงานบทความนี้อาจช่วยคุณได้
  • เปลี่ยนชื่อหลายไฟล์ในโฟลเดอร์
  • พวกเราส่วนใหญ่อาจประสบกับปัญหานี้ที่เราต้องเปลี่ยนชื่อไฟล์หลาย ๆ ไฟล์ในโฟลเดอร์การเปลี่ยนชื่อไฟล์ทีละชื่อจะทำให้เราแทบคลั่งหากมีไฟล์หลายร้อยหรือหลายพันไฟล์ในโฟลเดอร์นั้น มีฟังก์ชั่นดีๆให้เราจัดการกับงานนี้หรือไม่?
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