Solved

filefilter in Excel VBA not working as I want

Posted on 2014-09-25
6
107 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now