Select multiple files to open, then later open them

RockandGrohl

Active Member
Joined
Aug 1, 2018
Messages
497
Office Version
  1. 2010
Platform
  1. Windows
Hi all, I have this code within a userform called CPanel.

You press a button and navigate to one of a few folders that contain reports in. You can select one or multiple files. Upon selection, the filenames for the reports are written in a text-box with a carriage return in between them, to allow the user to review the files chosen, see below.

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
            CampList.Text = fileName & vbCrLf
        Next oFD
        On Error GoTo 0
    End With
       
    Set fd = Nothing


Once the user is satisfied with the files selected, they press a button to open each sheet in turn and import the data into the main sheet "Import"

This is where I'm getting stuck. My list of files will be something like this, with only the filenames shown.

1. Big Report 21-02-2020.xlsx
2. Coach report 04-05-2020.xlsx
3. Christmas report 11-11-2020.xlsx

Should I preface each opening with a variable string pName (pathname) that ideally would be chosen for where the files are opened, right? Like below
VBA Code:
pName = Left(fileName, InStr(fileName, "\") + 1)
(Obviously I'd jiggle the +1 depending on what gets spat out at me)

So I need to loop through the reports, open the first one, do the data shenanigans (I should be ok writing this code) and then close the workbook and find the next filename in the list. This is where I'm getting stuck, because I don't know how to split one or more strings by the carriage return? I essentially want to write something like:

VBA Code:
Do until list of all workbooks is exhausted
Set wb = workbooks.open(pName & [somehow determined first file name])
'doing all my copy-pasting stuff here
Wb.close 
Loop


Lol.. What a mess of a question. Cheers.
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,757
Office Version
  1. 365
Platform
  1. Windows
I would suggest putting the file names into a listbox, rather than a textbox. Is that ok?
 

RockandGrohl

Active Member
Joined
Aug 1, 2018
Messages
497
Office Version
  1. 2010
Platform
  1. Windows
I would suggest putting the file names into a listbox, rather than a textbox. Is that ok?

Would the list box show all items in one go, it won't have a drop down?

Cheers.
 

Attachments

  • Example.JPG
    Example.JPG
    34.2 KB · Views: 2

RockandGrohl

Active Member
Joined
Aug 1, 2018
Messages
497
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Cool, I've now got this but it's having a wobbly:

VBA Code:
    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
            pName = Left(fileName, InStrRev(fileName, "\"))
            'CampList.Text = CampList.Text & Mid(fileName, InStrRev(fileName, "\") + 1) & vbCrLf
            ListBox1.AddItem = " & Mid(fileName, InStrRev(fileName, " \ ") + 1) & "
            'CampList.Text = fileName & vbCrLf
        Next oFD
        On Error GoTo 0
    End With
       
    Set fd = Nothing

It now says expected function or variable :(
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,757
Office Version
  1. 365
Platform
  1. Windows
Replace your loop with this
VBA Code:
      For i = 1 To .SelectedItems.Count
         Me.ListBox1.AddItem .SelectedItems(i)
      Next i
 

RockandGrohl

Active Member
Joined
Aug 1, 2018
Messages
497
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Runtime error 424 - Object Required

Also, what I need to do is trim the path down to just show the file name, then later when I loop through to open them, I'm planning to append it with workbooks.open = pName & Listbox Item 1, etc.
 

nemmi69

Active Member
Joined
Mar 15, 2012
Messages
482
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
Here is a way to break up the string if you do not want to use SPLIT and put the names in an array

VBA Code:
Sub fgfgf()
Dim Tstr As String
Dim TrimStr As String
Dim ExctStr As String
Dim CrLoc As Integer
Dim TCell As Integer

Tstr = "Bob" & vbCrLf & "Tom" & vbCrLf & "Harry" & vbCrLf & "****" & vbCrLf
TrimStr = Tstr
TCell = 0
CrLoc = InStr(1, TrimStr, vbCrLf)

Do While CrLoc > 0
    
    'Find name
    ExctStr = Left(TrimStr, CrLoc - 1)
    'The extracted name can now be used. In this case write to cells
    TCell = TCell + 1
    Range("A" & TCell).Value = ExctStr
    'remove used name
    TrimStr = Right(TrimStr, Len(TrimStr) - CrLoc)
    CrLoc = InStr(1, TrimStr, vbCrLf)

Loop
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
52,757
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
   Dim fd As FileDialog
   Dim i As Long

   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 i = 1 To .SelectedItems.Count
         Me.ListBox1.AddItem Split(.SelectedItems(i), "\")(UBound(Split(.SelectedItems(i), "\")))
      Next i
      Me.TextBox1.Value = Left(.SelectedItems(1), InStrRev(.SelectedItems(1), "\") - 1)
   End With
   
   Set fd = Nothing
If you will always be selecting files from the Sales folder then you can get rid of the textbox
 

RockandGrohl

Active Member
Joined
Aug 1, 2018
Messages
497
Office Version
  1. 2010
Platform
  1. Windows
I'll give your code a shot, but I will be selecting files from one of three locations. However, once a file is selected that location will be the only location for this process, if that makes sense?
 

Watch MrExcel Video

Forum statistics

Threads
1,122,246
Messages
5,595,044
Members
413,963
Latest member
teggl97

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
Top