logo
search
VBA & Macro Problems

How to Use VBA Wildcard Search to Find Words Not Followed by a Hyphen

Maira MehtabMaira Mehtab Sep 21, 2026 871 views

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-'.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor in Microsoft Word.

2
Define the wildcard search

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.

3
Set the replacement string

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.

4
Handle the hyphenated version separately

Create a subsequent, separate non-wildcard replacement routine to specifically handle 'Support-' if those instances also need to be modified.

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. 1. Download WPS Office: Download and install the latest version of WPS Office from the official website.
  2. 2. Open your document: Launch WPS Writer or WPS Spreadsheet and open the document you wish to automate.
  3. 3. Access the VBA Editor: Navigate to the 'Tools' tab on the ribbon and click on 'Developer' to open the VBA Editor.
  4. 4. Run your macro: Paste your wildcard search macro into a new module and execute the code to instantly process your text.
Fully compatible with Microsoft Word and Excel VBA scriptsBuilt-in VBA editor for writing, debugging, and running macrosLightweight and fast performance for heavy automated document tasksSupports advanced wildcard search and replace operations natively
microsoft office alternative - wps office

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'.