Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

filefilter in Excel VBA not working as I want

Posted on 2014-09-25
6
Medium Priority
?
144 Views
Last Modified: 2014-09-26
I have the following line in my VBA code in Excel 2010

    rnl_fname = Application.GetOpenFilename(Title:="Select R560 Renewal SUM", _
        filefilter:="R560Combined???.SUM (*.SUM),*.SUM")

When the file dialog box comes up, I only want it to show files that start with R560Combined and end in .SUM.  For instance I might have a R560CombinedARD.SUM and a R560ARD.SUM.  I only want to see the COMBINED one in my file open dialog.  I've tried putting the R560Combined in front of both *.sum and that doesn't work.  if I put it in front of the first *.sum, then it defaults to *.* basically and doesn't use my filter.  Any ideas on how I can make this work?
0
Comment
Question by:pmac38CDS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:johnb25
ID: 40344987
Hi,

This should do it:
    rnl_fname = Application.GetOpenFilename(Title:="Select R560 Renewal SUM", _        filefilter:="R560Combined (*.SUM),R560Combined*.SUM")

John
0
 
LVL 1

Author Comment

by:pmac38CDS
ID: 40345941
I tried that and it still shows me both file names.  It shows R560CombinedARD.SUM and a R560ARD.SUM as available files.  There must be something I am missing.
0
 
LVL 6

Expert Comment

by:johnb25
ID: 40346353
I have looked into this a bit more.
It looks like you can't add a partial filter with the GetOpenFileName function.
The code below does work for me.
When I tested the previous code I did not notice all my files started with the same letters, so I did not have a proper test of the filter.

John

Sub OpenWorkbook()
Dim fd As FileDialog
Dim rnl_fname As String
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.InitialFileName = "R560Combined*"
.Filters.Clear
.Filters.Add "R560Combined", "*.SUM"
.FilterIndex = 1
End With
If fd.Show = -1 Then
rnl_fname = fd.SelectedItems(1)
Workbooks.Open (rnl_fname)
End If
End Sub

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:pmac38CDS
ID: 40346375
Okay that works but it now shows me other files that don't have the .sum extension.  For instance in that same folder I have R560CombinedXXX.SUM and R560CombinedXXX.PRN.  It's only showing the combined files now, but it shows both sum and prn extensions.
0
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 2000 total points
ID: 40346436
Try this:
Sub GetSUMFile()
    Dim strFilePathName As String
    Dim boolGotFile As Boolean
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .Title = "Select R560 Renewal file"
        .Filters.Add "R560Combined", "*.SUM"
        .FilterIndex = 1
        .AllowMultiSelect = False
        .InitialFileName = "R560Combined???.SUM"
        boolGotFile = .Show
        
        If boolGotFile Then
            strFilePathName = Trim(.SelectedItems.Item(1))
        End If
    End With
    
    'if you just want the filename
    'strFileName = Mid(strFilePathName, InStrRev(strFilePathName, "\") + 1, 100)
End Sub

Open in new window


Regards,
-Glenn
0
 
LVL 1

Author Closing Comment

by:pmac38CDS
ID: 40346624
This worked perfectly!!  Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question