Skirmante Valentaite
Skirmante Valentaite

Reputation: 105

Select multiple folders - VBA

I'm using macro to merge CSV files into one. Macro is working good, but I have one block, that allows to select a folder with CSV files to merge.

Two questions:

  1. Can I choose more than one folder to merge? (In one folder I have a lot of CSV files.).
  2. How A can choose other initial directory from where to choose the folders? I need use folders from server.

Code block:

'Browse to the folder with CSV files
Set oApp = CreateObject("Shell.Application")
Set oFolder = oApp.BrowseForFolder(0, "Select folder with CSV files", 512)
If Not oFolder Is Nothing Then
    foldername = oFolder.Self.Path
    If Right(foldername, 1) <> "\" Then
        foldername = foldername & "\"
    End If

now my availability to choose one folder from:

Not enough choice

I would love to choose from more like this:

More choice

Upvotes: 2

Views: 6964

Answers (2)

Pᴇʜ
Pᴇʜ

Reputation: 57683

Selecting multiple folders is not possible at all. Even selecting two folders at the same time will not work.

You can select …

Here is how to select one folder:

Public Sub SelectFolder()
    Dim fdl As FileDialog

    Set fdl = Application.FileDialog(msoFileDialogFolderPicker)
    With fdl
        .InitialFileName = "C:\Temp" 'where we start choosing a folder
        If .Show <> -1 Then
            MsgBox "canceled" 'nothing was selected
        Else
            Debug.Print .SelectedItems(1) 'the selected folder
        End If
    End With
End Sub

Upvotes: 3

FunThomas
FunThomas

Reputation: 29171

You can use Application.FileDialog. This allows to browse the folder and select files.

Dim fileCount As Long 

' Open the file dialog 
With Application.FileDialog(msoFileDialogOpen) 
    .AllowMultiSelect = True 
    .Filters.Clear
    .Filters.Add "CSV files", "*.csv"
    .Show 

    For fileCount = 1 To .SelectedItems.Count 
        Debug.print .SelectedItems(fileCount) 
    Next fileCount  

End With 

Upvotes: 3

Related Questions