Code to enable user to select multiple worbooks

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following code that enables a user to select a workbook in a particular folder


I would like this amended so that the user can select which workbooks he/she wants to select


Code:
 Sub Open_bank()
ChDir "C:\bank"
Dim nb As Workbook, ts As Worksheet, A As Variant
       
    Set ts = ActiveSheet
      A = Application.GetOpenFilename
    If A = False Or IsEmpty(A) Then Exit Sub
    
    Application.ScreenUpdating = False
    
    Set nb = Workbooks.Open(A)
    ThisWorkbook.Activate
 
End Sub


Your assistance in this regard is most appreciated
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try

Code:
Dim SelectedFileNames as Variant
Dim oneFileName as Variant

SelectedFileNames = Application.GetOpenFileName(MultiSelect:=True)

if SelectedFileNames = False then Exit Sub

For each oneFileName in SelectedFileNames
    Workbooks.Open oneFileName
Next oneFileName
 
Upvote 0
Thanks for the help, much appreciated
 
Upvote 0
When I open several files I get type mismatch and the following code is highlighted


Code:
 If SelectedFileNames = False Then Exit Sub

Kindly amend code so that I can select more than one file to open
 
Last edited:
Upvote 0
hi,
see if following does what you want:

Code:
Sub OpenMultiSelection()
    Dim SelectedFiles As Variant
    Dim i As Integer

    SelectedFiles = Application.GetOpenFilename(MultiSelect:=True)
    If VarType(SelectedFiles) = vbBoolean Then Exit Sub
    
    For i = LBound(SelectedFiles) To UBound(SelectedFiles)
        Workbooks.Open Filename:=SelectedFiles(i)
    Next i

End Sub


Dave
 
Last edited:
Upvote 0
Hi Dave

Thanks for the help. Have just tested your code & it works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,419
Messages
6,119,389
Members
448,891
Latest member
tpierce

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