Solved

filefilter in Excel VBA not working as I want

Posted on 2014-09-25
6
110 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

895 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

12 Experts available now in Live!

Get 1:1 Help Now