filtering Application.GetOpenFilename

SeniorTom

Board Regular
Joined
Jun 5, 2017
Messages
96
I would like to display only certain .xl files, eg *roster*.xls* I have tried both sides of the filter and the best I get is all .xls*. Can I narrow it down to only files with roster in the name?


Code:
 ' Set up file listings
    FileInfo = "EXCELL files (*.xls*),*.xl*"
    ' Set Dialog Box caption
    Title = "         *****************************  Select the Roster file to use *****************************"
    'Display the .xl files to select the roster file
    ' shows all the .xl* files in local directory
    fullpath = Application.GetOpenFilename(FileInfo, , Title)
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
SeniorTom,

Instead of .GetOpenFilename, you might consider .FileDialog...

Code:
Sub LimitFileSelection()
Dim fd As FileDialog
Dim Surch As String
Dim i As Long
Dim wb As Workbook
Surch = "roster"

''''Open FileDialog and select file(s)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
    .Title = "         *****************************  Select the Roster file to use *****************************"
    .InitialView = msoFileDialogViewList
    .AllowMultiSelect = True
    .InitialFileName = "*" & Surch & "*"
    If .Show = 0 Then Exit Sub
End With
''''Loop through selected file(s)
For i = 1 To fd.SelectedItems.Count
    Set wb = Workbooks.Open(fd.SelectedItems(i))
    ''''More code
    wb.Close savechanges:=False
Next i
End Sub



Cheers,

tonyyy
 
Last edited:
Upvote 0
Tonyyyy,

Your routine is great, but if the file is already open, I have a problem. I can check if it open but 'am having a problem setting the "set WB = fd.SelectedItems(i)" I get type mismatch. Can you guide me?
 
Upvote 0
...if the file is already open, I have a problem.

What's the error number and description? And which line of code is highlighted when the error occurs?
 
Upvote 0
..."set WB = fd.SelectedItems(i)" I get type mismatch...

The error occurs because "fd.SelectedItems(i)" returns the full file path, and "set WB" in this case is only looking for the file name. You might try...

Code:
For i = 1 To fd.SelectedItems.Count
    On Error Resume Next
        fName = fd.SelectedItems(i)
        fName = Right(fName, Len(fName) - InStrRev(fName, "\"))
        Set wb = Workbooks(fName)
    On Error GoTo 0
    
    If wb Is Nothing Then
        Set wb = Workbooks.Open(fd.SelectedItems(i))
        MsgBox "pause"
        ''''More code
        wb.Close savechanges:=False
    Else
        MsgBox wb.Name & " is open"
        ''''More code
    End If
Next i
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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