+ Reply to Thread
Results 1 to 5 of 5

Changing filters in a dialog box?

Hybrid 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.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,296

    Re: Changing filters in a dialog box?

    Not using msoFileDialogFilePicker, unfortunately.

    You can read the files of a selected directory and add them to a list on a userform if they meet your needs eg


    Public strF As String
    
    Sub UserformCode()
        
        Dim fNames() As String
        Dim objFSO As Scripting.FileSystemObject
        Dim objFolder As Scripting.Folder
        Dim colFiles As Scripting.Files
        Dim objFile As Scripting.File
        Dim i As Integer
    
        Set objFSO = New FileSystemObject
        strF = "M:\My Documents\Excel"
        Set objFolder = objFSO.GetFolder(strF)
        Set colFiles = objFolder.Files
    
        For Each objFile In colFiles
            If LCase(objFile.Name) Like "user*.h" Then
                ReDim Preserve fNames(0 To i)
                fNames(i) = objFile.Name
                i = i + 1
            End If
        Next objFile
      
        Load UserForm1
    
        UserForm1.ListBox1.List = fNames
    
        UserForm1.Show
        
    End Sub
    and use code like this on the userform:

    Private Sub CommandButton1_Click()
    UserForm1.Hide
    With UserForm1.ListBox1
       For i = 0 To .ListCount - 1
          If .Selected(i) = True Then
             myPath = strF & "\" & .List(i)
          End If
       Next i
    End With
    Unload UserForm1
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Forum Contributor
    Join Date
    03-07-2013
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    274
    Quote Originally Posted by Bernie Deitrick View Post
    Not using msoFileDialogFilePicker, unfortunately.

    You can read the files of a selected directory and add them to a list on a userform if they meet your needs eg


    Public strF As String
    
    Sub UserformCode()
        
        Dim fNames() As String
        Dim objFSO As Scripting.FileSystemObject
        Dim objFolder As Scripting.Folder
        Dim colFiles As Scripting.Files
        Dim objFile As Scripting.File
        Dim i As Integer
    
        Set objFSO = New FileSystemObject
        strF = "M:\My Documents\Excel"
        Set objFolder = objFSO.GetFolder(strF)
        Set colFiles = objFolder.Files
    
        For Each objFile In colFiles
            If LCase(objFile.Name) Like "user*.h" Then
                ReDim Preserve fNames(0 To i)
                fNames(i) = objFile.Name
                i = i + 1
            End If
        Next objFile
      
        Load UserForm1
    
        UserForm1.ListBox1.List = fNames
    
        UserForm1.Show
        
    End Sub
    and use code like this on the userform:

    Private Sub CommandButton1_Click()
    UserForm1.Hide
    With UserForm1.ListBox1
       For i = 0 To .ListCount - 1
          If .Selected(i) = True Then
             myPath = strF & "\" & .List(i)
          End If
       Next i
    End With
    Unload UserForm1
    End Sub
    Hi Bernie,
    For me it's quite an overkill of code ...
    I'm sort of new to VBA and wanted to keep it as
    simple as possible.

    One thing though, the only reason I wanted to
    filter the files is because I wanted the user to pick
    any file he sees but wanted to limit the files.
    in reality all I need is the path to the folder !
    I really strip off the User.h file and I use the path of
    the folder.

    So with my original code can I just go about
    getting only the path name without the user picking a file?

    For eample I saw this:

    http://software-solutions-online.com/2014/03/13/vba-save-file-dialog-filedialogmsofiledialogsaveas/

    But what does this mean:

    strPath = _

    thanks
    for your reply
    Rn
    Last edited by RustyNail; 05-01-2015 at 03:28 PM.

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,296

    Re: Changing filters in a dialog box?

    You can get the path from the file name like this - but they do have to select a file to get a returned value.

    Sub test()
        SelectFiles 2
    End Sub
    Sub SelectFiles(FilterType As Integer)
        Dim iFileSelect As FileDialog
        Dim myPath As String
        
        
        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", "*.h"
        End If
        
        ' ==================================================
        
        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
        MsgBox "The path is " & Left(myPath, InStrRev(myPath, "\") - 1)
        'Set object variable to Nothing
        Set iFileSelect = Nothing
        
    End Sub

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

    Re: Changing filters in a dialog box?

    Thanks Bernie

    I will just have to live with it for now.... let the user pick any file .....

    regards
    Rn

+ Reply to Thread

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