VBA & Macro Problems

How to Filter GetOpenFilename by File Name and Extension in Excel VBA

Nimra MalikNimra Malik Nov 2, 2026 869 views

Question details

The user needs a way to filter the file selection dialog in VBA to only show or accept files that match both a specific file extension and a specific text string within the file name.

How to Filter GetOpenFilename by File Name and Extension in VBA
Product
Microsoft Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Prompting a user to select a file via a macro, but ensuring they can only choose files containing specific keywords in the file name alongside a matching file extension.
Observed behavior
The standard Application.GetOpenFilename method successfully filters by file extension (e.g., *.xlsx) but lacks a built-in parameter to natively filter files by partial text strings contained in their names.
Before you start

Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have the Developer tab enabled to access the VBA editor.

Solution 1Recommended

Use GetOpenFilename and Validate the File Name Post-Selection

Use the standard dialog to filter the extension, then use VBA string functions to verify if the chosen file's name contains the required text before proceeding.

Since the Windows file dialog triggered by VBA does not support wildcard name filtering embedded alongside extension filtering, the most efficient workaround is to let the user pick a file of the correct type, and then validate their choice via code.

1
Open the VBA Editor

Press Alt + F11 to open the VBA editor and insert a new Module.

2
Call GetOpenFilename

Declare a Variant variable and assign it the result of Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx").

3
Check for cancellation

Add an If statement to exit the sub if the user clicked Cancel (e.g., If filePath = False Then Exit Sub).

4
Validate the file name

Use the InStr function to check the selected path. For example: If InStr(1, filePath, "YourKeyword", vbTextCompare) = 0 Then MsgBox "Invalid file name." Else 'Proceed with your code.

Use GetOpenFilename and Validate the File Name Post-Selection
Best Practice: You can place the GetOpenFilename call inside a Do...Loop until a valid file is chosen or the user explicitly cancels.
Advanced Macro Support

Easily Run VBA Macros to Filter Files Using WPS Spreadsheet

WPS Spreadsheet features a highly compatible built-in VBA editor. You can seamlessly run your GetOpenFilename scripts, build custom UserForms, and automate your workflows exactly as you would in Microsoft Excel.

  1. 1. Install WPS Office: Download and install the free WPS Office suite, then open your macro-enabled workbook (.xlsm).
  2. 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon and click 'VBA Editor' to view your scripts.
  3. 3. Paste your macro code: Insert your post-selection validation macro or your custom UserForm directly into the workspace.
  4. 4. Run your file filter: Execute the macro natively within WPS Spreadsheet to seamlessly prompt users for filtered files.
Fully compatible with Microsoft Excel VBA syntax and functions like GetOpenFilename.Supports custom UserForms for advanced file filtering interfaces.Lightweight installation with powerful macro execution capabilities.Free to use for everyday spreadsheet tasks and automation.
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't GetOpenFilename("*.xlsx*keyword*") work in VBA?

The built-in file filter for GetOpenFilename relies on the standard Windows file dialog interface, which only parses file extensions or exact file types. It does not support wildcards embedded within the main file name string.

Can I use Application.FileDialog(msoFileDialogFilePicker) to filter by name?

No, Application.FileDialog shares similar limitations regarding name filtering. While it offers more robust multi-selection features, its filter lists are still restricted to extensions. You will still need to use post-selection string validation.

How do I extract just the file name from the full path returned by GetOpenFilename?

You can isolate the file name by using the Dir function on the returned path string, or by using the InStrRev function to locate the last backslash (\) and extracting the substring that comes after it.