How to Use VBA Wildcard Search to Find Words Not Followed by a Hyphen
Question details
The user needs to create a VBA search-and-replace routine that accurately finds a specific word (e.g., 'Support') only when it is not immediately followed by a hyphen.
- Product
- Microsoft Word and Excel
- Device & OS
- not provided
- Scenario
- Writing a VBA macro to perform a complex search-and-replace operation without accidentally modifying words attached to a hyphen.
- Observed behavior
- Standard search routines improperly identify or replace the word even when it is part of a hyphenated phrase like 'Support-'.
Verify whether your VBA macro is targeting Word documents or Excel spreadsheets, as text boundary behaviors and wildcard support differ significantly between the two applications.
Use Character Class Wildcards in VBA (Word)
This is the most robust method for Word documents, using the [!-] character class to explicitly exclude hyphens while capturing trailing spaces or paragraph marks.
By utilizing Word's advanced wildcard feature in your VBA macro, you can specify exactly which characters are allowed or forbidden immediately following your target word.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in Microsoft Word.
Set up your Find text string as 'Support([!-])'. The '[!-]' syntax commands the search to match any character except a hyphen, effectively capturing spaces, punctuation, or paragraph marks.
Use '\1' in your replacement text (e.g., 'NewText\1'). The '\1' variable retains the character captured by the parentheses so you do not accidentally delete trailing spaces or paragraph returns.
Create a subsequent, separate non-wildcard replacement routine to specifically handle 'Support-' if those instances also need to be modified.
Search Using Paragraph Marks (vbCr) in Word
This approach is useful if the target word always appears at the very end of a paragraph rather than inline with other text.
Adjust Search Logic for Excel Macros
Because Excel cell values do not naturally contain trailing paragraph marks like Word, wildcard routines must be adapted for spreadsheet contents.
Run Your VBA Macros Seamlessly in WPS Office
WPS Office offers robust support for VBA macros, allowing you to execute complex search-and-replace wildcard routines in both Writer and Spreadsheets with high efficiency.
- 1. Download WPS Office: Download and install the latest version of WPS Office from the official website.
- 2. Open your document: Launch WPS Writer or WPS Spreadsheet and open the document you wish to automate.
- 3. Access the VBA Editor: Navigate to the 'Tools' tab on the ribbon and click on 'Developer' to open the VBA Editor.
- 4. Run your macro: Paste your wildcard search macro into a new module and execute the code to instantly process your text.

Frequently Asked Questions
What does [!-] mean in a VBA wildcard search?
In a VBA wildcard search, brackets denote a character class, and the exclamation mark acts as a 'NOT' operator. Therefore, '[!-]' instructs the search to match any single character except a hyphen.
Why does my wildcard replacement delete the space after the word?
If you search for a word plus a trailing character without capturing it using parentheses, the replacement text will overwrite that trailing character. Put parentheses around the wildcard expression and use '\1' in the replacement box to keep the original spacing.
Can I use Word wildcard syntax in Excel VBA?
Not directly via the standard Excel Find/Replace method. Excel VBA relies on the 'Like' operator or regular expressions (RegEx) for advanced wildcard pattern matching, as it does not share Word's exact text boundary syntax.
What represents a paragraph mark in Word VBA?
In Word VBA, a standard paragraph mark is represented by the constant 'vbCr' (Carriage Return). In the standard Find and Replace dialog box, it is often represented by '^p' or '^13'.




