Macro to allow user to select Multiple files

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have code below that I need amended to limit selection to .xlsm files that contain EVT and must be latest modified files

The code works well, except does not limit the brosing to files containing EVT, being the latest modified files

Kindly amend my code

Code:
 Sub Open_Workbook()
ChDir ("C:\SalesReport by Month")
A:
Dim A     As Variant
Dim LR As Long
     A = Application.GetOpenFilename(MultiSelect:=True)
    If TypeName(A) = "Boolean" Then Exit Sub
        
    Dim file As Variant
        
    Application.ScreenUpdating = False
   For Each file In A
        With Workbooks.Open(file)
    
        
        With Sheets(2)
            .Range("a1", .Range("X" & Rows.Count).End(xlUp)).Copy _
                Destination:=ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1)
            
            
        End With
    
        .Close SaveChanges:=False
        
       
    End With
 Next
 With Sheets(2)
  .Range("A1").EntireRow.Delete
  End With
  
 Application.ScreenUpdating = True
 
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
I have managed to amend my code to limit the browsing to files starting with "EVT" .xlsm files, but need code below amended to only display the latest modified files


It would be appreciated if someone could kindly advise me

Code:
 Sub Open_Workbook()
    Dim folderPath As String
    folderPath = "C:\SalesReport by Month"

    Dim fd As fileDialog
    Set fd = Application.fileDialog(msoFileDialogFilePicker)

    With fd
        .ButtonName = "Select"
        .Filters.Clear
        .Filters.Add "Excel Macro-Enabled Workbook", "*.xlsm"
        .InitialFileName = folderPath & "*EVT*.xlsm"
        .Title = "Select XLSM files"
        .AllowMultiSelect = True

        Dim file As Variant

        Application.ScreenUpdating = False

        If .Show = -1 Then ' User pressed OK in the dialog box
            For Each file In .SelectedItems
                With Workbooks.Open(file)
                    With .Sheets(2)
                        .Range("A1", .Range("X" & .Rows.Count).End(xlUp)).Copy _
                            Destination:=ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1)
                    End With
                    .Close SaveChanges:=False
                End With
            Next file
        Else ' User pressed Cancel in the dialog box or no files were selected
            Exit Sub
        End If
    End With

    With Sheets(2)
        .Range("A1").EntireRow.Delete
    End With

    Application.ScreenUpdating = True
End Sub
Sub ExtractDate()
With Sheets("Dates")
.Range("A2").FormulaR1C1 = "='Imported Data'!RC[1]"
.Range("B2").FormulaR1C1 = "='Imported Data'!RC[1]"
   End With
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,102
Messages
6,123,097
Members
449,096
Latest member
provoking

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