How to Create Folders based on Cell Values with VBA

,

This post will guide you how to create folders based on cell values in the current worksheet in Excel. How do I automatically create folders based on cell values with VBA macro in Excel. How to use VBA macro to create folders bsed on cell values in Excel.

1. Create Folders based on Cell Values

Assuming that you have cell values in range of cells B1:B6, and you want to create folders based on those cell values in the current workbook directory. How to quickly achive the result. You can create an Excel VBA macro to quickly create folders based on the selected cell values. Just do the following steps:

#1 open your excel workbook and then click on “Visual Basic” command under DEVELOPER Tab, or just press “ALT+F11” shortcut.

Get the position of the nth using excel vba1

#2 then the “Visual Basic Editor” window will appear.

#3 click “Insert” ->”Module” to create a new module.

convert column number to letter3

#4 paste the below VBA code into the code window. Then clicking “Save” button.

Sub MakeFolders()
    Dim dirName As String
    Dim selectedRange As Range
    Dim destinationPath As String
    Dim cell As Range
    Dim i As Long

    ' Prompt user to select a range of cells
    Set selectedRange = Application.InputBox("Select a range of cells:", "Select Range", Type:=8)

    ' Check if a range was selected
    If Not selectedRange Is Nothing Then
        ' Prompt user to select a destination folder
        With Application.FileDialog(msoFileDialogFolderPicker)
            .AllowMultiSelect = False
            If .Show = -1 Then
                destinationPath = .SelectedItems(1) & "\"
            Else
                MsgBox "No folder selected. Operation cancelled.", vbInformation
                Exit Sub
            End If
        End With

        ' Create folders based on cell values
        On Error Resume Next ' Enable error handling
        For Each cell In selectedRange
            dirName = cell.Value
            MkDir destinationPath & dirName ' Create folder with cell value as name
        Next cell
        On Error GoTo 0 ' Reset error handling

        MsgBox "Folders have been created successfully!", vbInformation
    Else
        MsgBox "No range selected. Operation cancelled.", vbInformation
    End If
End Sub

#5 back to the current worksheet, then run the above excel macro. Click Run button.

When prompted, select the range of cells in your workbook that contain the values you want to use as folder names.

Then, when prompted to select a destination folder, choose the location where you want the folders to be created.

After running the macro, check the destination path to see if the folders have been created successfully.

2. Video: Create Folders based on Cell Values

This Excel video tutorial , we’ll learn how to create folders based on cell values directly from an Excel worksheet using VBA Macro.

Leave a Reply