Macro to open File in Folder using Wildcard

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,595
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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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