Macro to open several Files

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have a macro to open a workbook using a wilcard Character


I would like the code amended to open 3 workbooks starting with

The workbooks to be opened are all .xlsm workbooks

Bauten East
Prolen South
Prolem North


Eg Bauten East Sales Oct 2022.xlsm


I need a wildcard Character so to limit the files to be selected in "C:\Sales Reports by month"


Your assistance is most appreciated



Code:
 Sub Open_Workbook()

Dim xfile As Variant

Dim wb2 As Workbook


Application.ScreenUpdating = False



ChDir ("C:\Sales Reports by month")

With Application.FileDialog(msoFileDialogFilePicker)

.ButtonName = "Open"

.Filters.Clear

.Filters.Add "Excel Files", "*.xlsm"

.InitialFileName = "*Bauten East*.xlsm"

.Title = "Select File"

If .Show <> -1 Then Exit Sub

For Each xfile In .SelectedItems

Set wb2 = Workbooks.Open(xfile)

wb2.Sheets(2).UsedRange.Copy _

ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1)

wb2.Close False

Next

End With

ChDir ("C:\Sales Reports")

Application.ScreenUpdating = True

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi howard. Maybe this code will help. You need to adjust the folder/directory path to suit. HTH. Dave
Code:
Dim TempFileDialog As FileDialog
Set TempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With TempFileDialog
.AllowMultiSelect = True
.ButtonName = "Open"
.Title = "Select Files"
.InitialFileName = "C:\TestFolder\"
.Filters.Clear
.Filters.Add "Excel files", "*.xlsm"
.Filters.Add "All files", "*.*"
.Show
'If TempFileDialog.Show = -1 Then
'Loop through all selected workbooks
'For i = 1 To TempFileDialog.SelectedItems.Count
End With
 
Upvote 0
I should have added that you need to hold the shift button while selecting the files for multiselect. Also, I should have further commented how to use the selected files. Dave
Code:
Dim i As Integer, wb As Workbook
If TempFileDialog.Show = -1 Then
'Loop through all selected workbooks
For i = 1 To TempFileDialog.SelectedItems.Count
Set wb = Workbooks.Open(TempFileDialog.SelectedItems(i))
'do stuff
Next i
End If
 
Upvote 0
Thanks for the help Dave

I want to linmit my selection in the folder to the workbooks starting with below :

Bauten East
Prolem South
Prolem North


Kindly amend your code to accomodate this
 
Upvote 0
If you just want those files, why use a file dialog at all? You can code directly to open only those files if you provide the entire file path for each file. If you want to use the file picker, you can trial this code. Dave
Code:
Sub Test()
Dim TempFileDialog As FileDialog
Dim i As Integer, wb As Workbook
Set TempFileDialog = Application.FileDialog(msoFileDialogFilePicker)
With TempFileDialog
.AllowMultiSelect = True
.ButtonName = "Open"
.Title = "Select Files"
.InitialFileName = "C:\TestFolder\"
.Filters.Clear
.Filters.Add "Excel files", "*.xlsm"
.Filters.Add "All files", "*.*"
End With
'ensure selection
If TempFileDialog.Show = -1 Then
'Loop through all selected workbooks
For i = 1 To TempFileDialog.SelectedItems.Count
If InStr(TempFileDialog.SelectedItems(i), "Bauten East") Or _
InStr(TempFileDialog.SelectedItems(i), "Prolem South") Or _
InStr(TempFileDialog.SelectedItems(i), "Prolem North") Then
Set wb = Workbooks.Open(TempFileDialog.SelectedItems(i))
'do stuff
End If
Next i
End If
End Sub
 
Upvote 0
Many Thanks Save

When using the file Pickers the file Dialog Box shows all the files in the folder

I only want to see files containing the following files below in the dialog box


Bauten East
Prolem South
Prolem North
 
Upvote 0
Yes it shows them all, but the code will only process those specific workbooks (if they are selected). I am unaware of how to only show those files in the file dialog box unless they are the only files in the folder. Dave
 
Upvote 0
HTH
VBA Code:
Sub sbOpenFiles()
    Application.ScreenUpdating = False
    Dim myPath
    myPath = "C:\Sales Reports by month\"
    
    Dim myFileName
    myFileName = Dir(myPath)
    
    Dim myCheck
    Do Until myFileName = ""
        If InStr(1, myFileName, ".xlsm") Then
            myCheck = InStr(1, myFileName, "Bauten East") + InStr(1, myFileName, "Prolen South") + InStr(1, myFileName, "Prolem North")
            If myCheck Then
                Application.Workbooks.Open myPath & myFileName
            End If
        End If
        myFileName = Dir()
    Loop
End Sub
 
Upvote 0
Many Thanks Save

When using the file Pickers the file Dialog Box shows all the files in the folder

I only want to see files containing the following files below in the dialog box


Bauten East
Prolem South
Prolem North
Thanks for all your effort Dave
 
Upvote 0
HTH
VBA Code:
Sub sbOpenFiles()
    Application.ScreenUpdating = False
    Dim myPath
    myPath = "C:\Sales Reports by month\"
   
    Dim myFileName
    myFileName = Dir(myPath)
   
    Dim myCheck
    Do Until myFileName = ""
        If InStr(1, myFileName, ".xlsm") Then
            myCheck = InStr(1, myFileName, "Bauten East") + InStr(1, myFileName, "Prolen South") + InStr(1, myFileName, "Prolem North")
            If myCheck Then
                Application.Workbooks.Open myPath & myFileName
            End If
        End If
        myFileName = Dir()
    Loop
End Sub
Many Thanks for the help HongRu

Your code opens the workbooks which I require

I need you to kindly amend your code, so as to copy sheet2 from each on the specified workbooks containing "Bauten East", "Prolen South" and "Prolem North" and to paste these one below each other on sheet "imported data" in the destination workbook

Your assistance in this regard is most appreciated


I added code after your code as follows:

Code:
  With Sheets(2)
            .UsedRange.Copy _
                Destination:=ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1)



See full code below

Code:
 Sub sbOpenFiles()

  
  Application.ScreenUpdating = False
    Dim myPath
    myPath = "C:\Sales Reports by month\"
   
    Dim myFileName
    myFileName = Dir(myPath)
   
    Dim myCheck
    Do Until myFileName = ""
        If InStr(1, myFileName, ".xlsm") Then
            myCheck = InStr(1, myFileName, "Bauten East") + InStr(1, myFileName, "Prolen South") + InStr(1, myFileName, "Prolem North")
            If myCheck Then
                Application.Workbooks.Open myPath & myFileName
            End If
        End If
        myFileName = Dir()
    Loop

      With Sheets(2)
            .UsedRange.Copy _
                Destination:=ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1)
              
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,499
Members
449,089
Latest member
Raviguru

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