Macro to open Directory and select File using a wildcard

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,570
Office Version
  1. 2021
Platform
  1. Windows
I have a macro to select a directory , but need the code .filers.add amended so as to select a csv file


Code:
 With fDialog
    .Filters.Clear
      .Filters.Add "Excel files", "*.csv"
  .InitialFileName = "C:\sales\*Used Sales*Report*.*
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi howard,

I was stunned when testing as when the criteria was met any file with any extension was listed although only csv were wanted (System Windows11, Office 2019). You might be better off creating a UserForm with a ListBox/ComboBox to choose the path and folder, one for for extensions and a Listbox to show the results. Or have the results displayed in a sheet using Dir and i.e. BeforeRight-Click event to open the file wanted.

Holger
 
Upvote 0
Hi Holger

I managed to resolve the issue



Code:
Sub Open_Workbook()

     With Sheets("Overaged Stock")
     .Range("A1:S1000").ClearContents
     End With
     
    Dim nb As Workbook, ts As Worksheet, A As Variant
    Dim rngDestination As Range
       Dim fDialog As Object, varFile As Variant
    Set ts = ActiveSheet
    Sheets("Demo Vehicles").Select
        
    On Error Resume Next
    Set rngDestination = Application.Range("'Overaged Stock'!A1")
    
    On Error GoTo 0
   If rngDestination Is Nothing Then Exit Sub  'User canceled
           MsgBox ("Select Overaged Report")
   Set fDialog = Application.FileDialog(3)
     
    With Application.FileDialog(msoFileDialogFilePicker)
.InitialFileName = "C:\downloads\*New*Stock*.*"  'change to suit one's needs
If .Show = 0 Then Exit Sub
A = .SelectedItems(1)
End With

     If A = False Or IsEmpty(A) Then Exit Sub
    
    
          Set nb = Workbooks.Open(Filename:=A, local:=True)
    ThisWorkbook.Activate
    
    
   nb.Sheets(1).Range("A:S").Copy
    rngDestination.PasteSpecial Paste:=xlPasteValues
    rngDestination.PasteSpecial Paste:=xlPasteFormats
    Application.CutCopyMode = False
  
    nb.Close savechanges:=False 'Close the source workbook
     
   
        Application.ScreenUpdating = False
    
End Sub
 
Upvote 0
Hi howard,

glad you found a way to meet your request.

You have codeline

VBA Code:
   Set fDialog = Application.FileDialog(3)

but continue with

VBA Code:
    With Application.FileDialog(msoFileDialogFilePicker)

instead of using fDialog?

Holger
 
Upvote 0
Thanks for pointing the code to be corrected
 
Upvote 0

Forum statistics

Threads
1,216,022
Messages
6,128,326
Members
449,441
Latest member
khankins

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