Solved

filefilter in Excel VBA not working as I want

Posted on 2014-09-25
6
113 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel if statement 3 15
Problem with a moving column in Excel 6 38
Getting rid of #VALUE! 7 22
vba to flip column 14 18
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…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

856 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