I have the folowing macro below to open a folder and the user then needs to select the workbook to be copied
I need someone to amend my code using a wildcard to narrow down the selection starting with BRT1 to open the file for eg. full name is BRT1 Sales_Report Sept 2022.xlsm. Need a wildcard as Month and year changes
I need someone to amend my code using a wildcard to narrow down the selection starting with BRT1 to open the file for eg. full name is BRT1 Sales_Report Sept 2022.xlsm. Need a wildcard as Month and year changes
Code:
Sub Open_File()
ChDir "C:\Sales Ledgers"
Dim LR As Long
With Application
.DisplayAlerts = False
.ScreenUpdating = False
.AskToUpdateLinks = False
With Sheets("Imported Data")
LR = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A1:AD" & LR).ClearContents
Dim fDialog As Object, varFile As Variant
Dim nb As Workbook, tw As Workbook, ts As Worksheet
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
.CutCopyMode = False
End With
Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set fDialog = Application.FileDialog(3)
ChDir "C:\Sales Ledgers\"
With fDialog
.Filters.Clear
.Filters.Add "Excel files", "*.xlsm*"
.Show
For Each varFile In .SelectedItems
Set nb = Workbooks.Open(Filename:=varFile, local:=True)
With Sheets(3)
.Range("A1:AD2000").Copy
ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues
ThisWorkbook.Sheets("Imported Data").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteFormats
End With
nb.Close False
Next
End With
With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
.CutCopyMode = True
End With
End With
End With
With Application
.DisplayAlerts = True
.ScreenUpdating = True
.AskToUpdateLinks = True
End With
End Sub