Select Multiple Files to Open, and place those filenames in a TextBox

Status
Not open for further replies.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
801
Office Version
  1. 365
Platform
  1. Windows
Hi guys, I've seen a few threads like this scattered around the forums but haven't found anything too definitive.

The below code allows me to open a default directory, select multiple files and set them as what I want. However, it doesn't seem to want to apply them to the text box "CampList" I have within a userform.

VBA Code:
Dim i As Integer


With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = True
        .InitialFileName = "\\chw-dc03\company\Sales"
        .Filters.Clear
        
        .Filters.Add "Excel Files", "*.xls*"
        
        If .Show = True Then
            For i = 1 To .SelectedItems.Count
            
            CampList.Text = SelectedItems(i)
                        
            Next i
        End If
        
End With

What I'd like to do is, say I select 3 files, have all three file names separated by carriage return listed in the text box.

The file would look something like this:

\\chw-dc03\company\sales\2. RT 2021 UK Preview (A1-A6) Report 20.10.2020.xlsx

I'm planning to grab the final portion of the filename string using this:

VBA Code:
Sp = Split(facml, "\")
camnam = Trim(Left(Sp(UBound(Sp)), InStr(1, Sp(UBound(Sp)), "mailing", vbTextCompare) - 1))

Which should grab the text after the final "\"

But for now, I just need to get the tour names loaded into the CampList text box. Thanks.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Cancel this, found some code online and adapted it:

VBA Code:
Dim fd As FileDialog
Dim oFD As Variant
Dim fileName As String

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .ButtonName = "Select"
        .AllowMultiSelect = True
        .InitialFileName = "\\chw-dc03\company\Sales"
        .Filters.Add "Excel Files", "*.xls*", 1
        .Title = "Please choose a DM Report to open"
        .InitialView = msoFileDialogViewDetails
        .Show
        
        For Each oFD In .SelectedItems
            fileName = oFD
            CampList.Text = CampList.Text & Mid(fileName, InStrRev(fileName, "\") + 1) & vbCrLf
        Next oFD
        On Error GoTo 0
    End With
       
    Set fd = Nothing

If Siddharth Rout is on these forums, then cheers!
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top