メインコンテンツへスキップ

Excel のデータのリストから複数のフォルダーとサブフォルダーを作成する

ワークシート範囲内のスタッフ名のリストがあり、各スタッフの情報を保存するための個別のフォルダーを作成するとします。各フォルダーを手動で作成すると、時間がかかる場合があります。ただし、このプロセスを迅速化する効率的な方法があります。このガイドでは、指定したセル値に基づいてフォルダーを迅速に生成するためのいくつかの方法を紹介します。

セルの値に基づいてフォルダーを作成する

VBA コードを使用してセル値に基づいてフォルダーとサブフォルダーを作成する


セルの値に基づいてフォルダーを作成する

このセクションでは、さまざまな方法を詳細に検討し、セル値のリストに基づいてフォルダーを迅速かつ簡単に作成するための包括的なステップバイステップの手順を示します。

MDコマンドとメモ帳を使用してリストからフォルダーを作成する

MD コマンドとメモ帳を使用して Excel のリストをフォルダーに変換することは、簡単なバッチ スクリプトと、物事を整理しておくための Excel のコツを組み合わせた賢いトリックです。この方法は、すべてを手作業で行う必要がなく、大量のフォルダーをすばやく作成する場合に最適です。このタスクを実行するためのステップバイステップのガイドは次のとおりです。

ステップ 1: MD コマンドを使用して数式を作成する

次の数式をコピーするか、最初のセル値 (たとえば B1) の隣の空白セルに入力し、塗りつぶしハンドルを下にドラッグして、すべてのリスト項目に数式を適用します。

="MD "&A1

ステップ 2: 数式をコピーしてメモ帳ファイルに貼り付けます。

  1. イベント Ctrlキー+ C MDコマンド式でセルをコピーします。
  2. Open メモ帳 を押し Ctrlキー+ V をクリックしてコマンドを新しいファイルに貼り付けます。

ステップ 3: メモ帳ファイルを .bat ファイルとして保存する

名前を付けて保存 File メモ帳のタブ 名前を付けて保存 ダイアログ ボックスで、複数のフォルダーを作成するディレクトリを選択し、このファイルの名前を 。バット 拡大。最後にクリックします Save ボタン。 スクリーンショットを参照してください:

ステップ 4: .bat ファイルをダブルクリックして複数のフォルダーを生成します

  1. メモ帳ファイルを閉じ、以前に .bat ファイルを保存したフォルダーに移動します。
  2. ファイルをダブルクリックすると、複数のフォルダーが一度に作成されるのがわかります。以下のデモをご覧ください。
 

強力なツール – Kutools for Excel を使用してリストからフォルダーを作成します

強力な Kutools for Excelさん セルの内容からフォルダを作成する この機能により、Excel リストから簡単かつ迅速にフォルダーを作成できるようになりました。ただし、基本的なフォルダーだけではありません。 Kutoolsを使用すると、複数レベルのサブフォルダーを含む複雑な構造を一度に作成することもできます。いくつかの簡単な手順を実行するだけで、Excel のデータを整理されたフォルダー システムに変換し、生産性を大幅に向上させることができます。

Note:こんな場合はこちらをご利用ください セルの内容からフォルダを作成する 特徴を教えてください Kutools for Excelをダウンロードしてインストールします 最初。

インストールした後 Kutools for Excel、をクリックしてください クツールズプラス > インポート・エクスポート > セルの内容からフォルダを作成する を開く セルの内容からフォルダを作成する ダイアログボックス:

  1. フォルダーを作成する際に基づいて作成するセル値を選択します。
  2. 次に、 ボタンでフォルダーの保存先フォルダーを指定します。
  3. 最後に、クリック OK

結果:

Kutoolsはシートからのリストを処理し、指定された宛先に各エントリのフォルダーを作成します。宛先フォルダーに移動して結果を確認します。スクリーンショットを参照してください:

ヒント:
  1. この便利な機能は、次のような場合にも役立ちます。 フォルダーとそのサブフォルダーを作成する 必要に応じて。これを行うには、バックスラッシュ記号 (\) を使用して各レベルを区切って、目的のフォルダーとサブフォルダーの名前をセルに入力する必要があります。各セルの内容は、フォルダーとサブフォルダーの目的の構造を設定するためのガイドとして機能します。

    次に、 セルの内容からフォルダを作成する この機能を使用すると、すべてのフォルダーとそのサブフォルダーが正常に作成されます。スクリーンショットを参照してください:
  2. この機能を適用するには、 Kutools for Excelをダウンロードしてインストールします 最初。
 

VBA コードを使用してリストからフォルダーを作成する

Excel で VBA コードを使用すると、リストからフォルダーを作成するという面倒なタスクを、迅速な自動プロセスに変換できます。このセクションでは、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. セル内に重複したエントリがある場合、コードを実行すると、それらの重複に対してフォルダーが 1 つだけ作成されます。
  2. このコードを頻繁に使用する場合は、ワークブックを次の場所に保存することを検討してください。 Excel マクロが有効なワークブック フォーマット。このアクションによりワークブック内のコードが保存されるため、今後はコードを再入力したり再インポートしたりすることなく、コードを直接実行できるようになります。

VBA コードを使用してセル値に基づいてフォルダーとサブフォルダーを作成する

場合によっては、Excel セル内のデータに基づいて、フォルダーだけでなく、それに対応するサブフォルダーも生成する必要がある状況に陥ることがあります。このタスクを実現するために、ここでは VBA コードを紹介します。

ステップ 1: データを準備する

まず、次のスクリーンショットのようにデータを入力し、メイン フォルダー名を最初の列に、サブフォルダーの名前を 2 番目の列に入力します。

手順 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. このコードは、メイン フォルダーとその第 1 レベルのサブフォルダーのみを作成する場合にのみ使用できます。
  2. このコードを頻繁に使用する場合は、ワークブックを次の場所に保存することを検討してください。 Excel マクロが有効なワークブック フォーマット。このアクションによりワークブック内のコードが保存されるため、今後はコードを再入力したり再インポートしたりすることなく、コードを直接実行できるようになります。

関連記事:

  • Excel のすべてのフォルダーとサブフォルダーを一覧表示する
  • 指定したディレクトリのすべてのフォルダとサブフォルダをワークシートに一覧表示するというこの問題に悩まされたことはありますか? Excelでは、特定のディレクトリ内のすべてのフォルダの名前を一度に取得するための迅速で便利な方法はありません。 タスクに対処するために、この記事はあなたを助けるかもしれません。
  • フォルダー内の複数のファイルの名前を変更する
  • 私たちのほとんどは、フォルダ内の複数のファイルの名前を変更する必要があるというこの問題に苦しんでいる可能性があります。ファイル名をXNUMXつずつ名前変更すると、そのフォルダに数百または数千のファイルがあると気が狂います。 このタスクに対処するための良い機能はありますか?
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