Results 1 to 5 of 5

Changing filters in a dialog box?

Threaded View

  1. #1
    Forum Contributor
    Join Date
    03-07-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    274

    Changing filters in a dialog box?

    Hello,

    The file filter in the following code works well. When I open the dialog box I see all the *.txt file types....

    Sub SelectFiles()
         Dim iFileSelect As FileDialog
        
        Set iFileSelect = Application.FileDialog(msoFileDialogFilePicker)
        Dim vrtSelectedItem As Variant
    
        iFileSelect.Filters.Clear
        iFileSelect.Filters.Add "Text files", "*.txt"
        
         If iFileSelect.Show = -1 Then
                
             For Each vrtSelectedItem In iFileSelect.SelectedItems
                    MyPath = vrtSelectedItem   ' MyPath will have the filename the program is exported to
              Next vrtSelectedItem
                
          End If
            
        'Set object variable to Nothing
        Set iFileSelect = Nothing
    
    End Sub


    However, the new SelectFiles() function needs to be flexible enough so that I can pass it a variable and based on that variable
    I can alter the file filter. Something like this:


    
    Sub SelectFiles(FilterType as integer)
         Dim iFileSelect As FileDialog
        
        Set iFileSelect = Application.FileDialog(msoFileDialogFilePicker)
        Dim vrtSelectedItem As Variant
    
        iFileSelect.Filters.Clear
         
    ' new part ============================================
        if FilterType = 1 then
            iFileSelect.Filters.Add "Text files", "*.txt"
        Elseif FilterType = 2 then
            iFileSelect.Filters.Add "User files", "User*.h"    
        EndIf
    
    ' ==================================================
    
         If iFileSelect.Show = -1 Then
                
             For Each vrtSelectedItem In iFileSelect.SelectedItems
                    MyPath = vrtSelectedItem   ' MyPath will have the filename the program is exported to
              Next vrtSelectedItem
                
          End If
            
        'Set object variable to Nothing
        Set iFileSelect = Nothing
    
    End Sub

    The above gives a runtime error 5 when FilterType = 2 ?

    However, the above works if I do:

    
     Elseif FilterType = 2 then
         iFileSelect.Filters.Add "User files", "*.h"    
     EndIf
    But I need it to filter files only starting with "User" ... so all files like : "User*.h" ???

    Is there straight forwards approach to this?

    Thanks all in advance for your help!
    Rn
    Last edited by RustyNail; 05-01-2015 at 12:05 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Marco for changing numerous pivot chart filters at once
    By Nick Simo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2012, 12:09 PM
  2. Changing the Pivot Table Filters based on Multiple Cells
    By dbnhc8 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 08-28-2012, 02:37 PM
  3. [Macro] Excel Query Connection with Inputbox for changing filters
    By JohnGaltnl in forum Access Programming / VBA / Macros
    Replies: 0
    Last Post: 07-19-2011, 04:24 AM
  4. Changing SaveAs Dialog Box
    By McNulty in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-31-2009, 11:54 AM
  5. changing options in delete dialog box
    By marthann in forum Excel General
    Replies: 1
    Last Post: 03-23-2006, 05:20 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1