Solved

filefilter in Excel VBA not working as I want

Posted on 2014-09-25
6
139 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

635 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