• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

File open dialog with filters

Costas

Member
Hi Chandoo,

I'm trying to filter the files that show on the open dialog box to include certain strings but also to be csv type.
Using the code below, the string filters work but not the file type. I've also tried to only filter on the file type but then I get all the csv files.

Code:
    MyPath = ThisWorkbook.Path
    ChDir MyPath
    
    With Application.FileDialog(msoFileDialogFilePicker)
          .Filters.Clear
          .InitialFileName = "*" & MySubName & "*" & MyLocation & "*.csv"
          .AllowMultiSelect = False
          'Filter file extension types
          .Filters.Add "Comma-Separated Value Files", "*.csv", 1
          .FilterIndex = 2
          'Show the dialog box
          .Show
          On Error GoTo ErrorHandler
          MyFullPath = .SelectedItems.Item(1)
      End With

Is it a case of either or between file name and file type or can I apply both filters?
 
Hi vletm,

I reread the reference you posted but it's very basic and doesn't deal with my specific issue.
Do you have any suggestions on the code?
 
Costas
Did You recheck that all those remarks works with Your specific issue
eg Use the * and ? wildcard characters when specifying the file name but not when specifying the path.
Suggestion ... try to use some basic filtering and let users select from those.
 
If you check my code, there are no wildcards on the path. The issue is filtering on the name and on the file type.
Do you know what specific changes I should make to fix the code?

Thanks for your suggestion but I can't follow it. If the user picks the wrong file, the information uploaded would be wrong and I would need to spend considerable time to clean the database.
 
Costas
You're using three times * ... or how?
Your If the user picks the wrong file ...
Do not let user pick - open only valid file without any Filedialog.
 
Vletm

The file naming conversion is IndustryType-CityName-YYYYMM. This is used for both xlsx files and csv files. I want the user to pick the correct month and year from the CSV files not the XLSX files. The problem is that both file types show and I do not want the user to pick and XLSX file. But I do want the user to choose the correct month and year from the CSV files. I hope this makes sense.
Do you know how to change the code to show only the CSV files?
 
Costas
Could You keep those csv-files in own folder?
Your If the user picks the wrong file ...
If user can even select from two csv-files then user could make mistake.
Could You code only correct path & filename?

I found some samples ... if You cannot do above.
Code:
With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select a file"
    .InitialFileName = path_which_has_those_files.  ' eg "C:\CSV\"
    .Filters.Clear
    .AllowMultiSelect = False
    .Filters.Add "Comma-Separated Value Files", "*.csv"
    .Show
     MyFullPath = .SelectedItems.Item(1)
End With
Ps. I cannot verify above code at all,
because that could work only with Windows-OS.
 
but then I get all the csv files
According to the post #2 link with the wildcard in the InitialFileName you should remove Filters.Add & FilterIndex codelines​
so that just needs 4 codelines in the With block …​
 
That worked Marc L. Thanks
Glad you removed your previous comment because it was unfair and insulting.
 
As it's never my will to insult anyone but maybe my english is a bit rough sometimes.​
As a reminder, the better initial post explanation, the more targeted answer …​
 
All good Marc. English is not my first language either so I understand where you are coming from.
Thanks for your help in resolving this.
 
Back
Top