How to Filter GetOpenFilename by File Name and Extension in Excel VBA
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.

- 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.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have the Developer tab enabled to access the VBA editor.
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.
Press Alt + F11 to open the VBA editor and insert a new Module.
Declare a Variant variable and assign it the result of Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx").
Add an If statement to exit the sub if the user clicked Cancel (e.g., If filePath = False Then Exit Sub).
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.

Create a Custom UserForm to Pre-Filter the File List
Build a customized file picker using a VBA UserForm and a ListBox that visually displays only the files matching both the name and extension criteria.
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. Install WPS Office: Download and install the free WPS Office suite, then open your macro-enabled workbook (.xlsm).
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon and click 'VBA Editor' to view your scripts.
- 3. Paste your macro code: Insert your post-selection validation macro or your custom UserForm directly into the workspace.
- 4. Run your file filter: Execute the macro natively within WPS Spreadsheet to seamlessly prompt users for filtered files.

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.




