Macro to open File in Folder using Wildcard

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,568
Office Version
  1. 2021
Platform
  1. Windows
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

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
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I managed to work out the code required

The is just after
ChDir "C:\Sales Ledgers\"


Code:
 Set tw = ThisWorkbook
Set ts = tw.ActiveSheet
Set fDialog = Application.FileDialog(3)
ChDir "C:\Sales Ledgers\"


Code:
 With Application.FileDialog(msoFileDialogFilePicker)
.InitialFileName = "C:\Sales Ledgers\BRT1*.xls*"
If .Show = 0 Then Exit Sub
A = .SelectedItems(1)
End With
 
Upvote 0

Forum statistics

Threads
1,215,758
Messages
6,126,718
Members
449,332
Latest member
nokoloina

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