VBA & Macro Problems

How to Find the Next Non-Space Character Using Word VBA

Algirdas JasaitisAlgirdas Jasaitis Oct 28, 2026 873 views

Question details

The user wants to detect the next character in a Word document that is not a space, while also properly distinguishing standard spaces from paragraph marks.

How to Find the Next Non-Space Character Using Word VBA
Product
Word
Device & OS
not provided
Scenario
Writing a VBA macro to navigate text and identify specific non-space and non-paragraph characters.
Observed behavior
The macro needs to identify character codes, specifically distinguishing spaces (ASCII 32) and paragraph marks (ASCII 13 or vbCr) to isolate the target character.
Before you start

Ensure you have the Developer tab enabled in your Word processor to access the VBA editor, and save your document before running custom macros to prevent unintended changes.

Solution 1Recommended

Use Character Codes to Detect Non-Space Characters

Collapse the current text selection, move forward one character at a time, and evaluate the ASCII character codes to skip spaces and paragraph marks.

In Word VBA, every character has an associated ASCII code. A standard space is represented by the code 32, while a paragraph mark is 13 (often represented as vbCr). By checking these values, your macro can easily skip them to find the next valid text character without needing to display a message to the user.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic Editor and insert a new Module where you will write the macro.

2
Collapse the Selection

Start your code by collapsing the current selection so it acts as a cursor. Use the command `Selection.Collapse Direction:=wdCollapseEnd`.

3
Loop Through Characters

Create a loop that moves the selection forward one character at a time using `Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend`.

4
Test the Character Code

Within the loop, test the selected character's code using `Asc(Selection.Text)`. Set the loop to continue as long as the ASCII value is 32 (space) or 13 (paragraph mark).

5
Identify the Target Character

Once the loop encounters a character with an ASCII value other than 32 or 13, the loop stops, leaving the selection on the first character that is neither a space nor a paragraph mark.

Use Character Codes to Detect Non-Space Characters
Searching Backward: You can modify this logic to search backward by collapsing the selection to the start (`wdCollapseStart`) and using `Selection.MoveLeft` to move through the text in reverse.
WPS VBA Editor

Write and Run VBA Macros Seamlessly in WPS Office

WPS Office provides robust support for VBA macros. You can execute character-detection scripts exactly as you would in Microsoft Word, ensuring high compatibility and smooth automation.

  1. 1. Enable Developer Tools: Open WPS Writer and navigate to the Developer tab on the top ribbon.
  2. 2. Access the VBA Editor: Click on 'Macros' or 'Visual Basic' to launch the integrated VBA Editor.
  3. 3. Execute Your Script: Paste your character detection code into a new module and click the Run button to test it on your document.
Fully compatible with Microsoft Word VBA scripts and character codes.Built-in Developer tab for easy macro recording, editing, and execution.Lightweight software architecture ensuring high performance for running complex macros.Free to download with comprehensive document processing features.
microsoft office alternative - wps office

Frequently Asked Questions

How do I test for a paragraph mark in Word VBA?

You can test for a paragraph mark by comparing the character against the constant `vbCr` or by checking if its ASCII character code using `Asc(character)` equals 13.

What is the ASCII value for a standard space?

The ASCII value for a standard space is 32. In a VBA macro, you can check this condition using the expression `Asc(Selection.Text) = 32`.

How can I move the selection without displaying a prompt?

Use the `Selection.Move` or `Selection.MoveRight` methods within your VBA code to quietly shift the cursor position. As long as you do not include `MsgBox` commands in your loop, the macro will run silently.

Does WPS Office support VBA character code checks?

Yes, WPS Office fully supports standard VBA syntax, including the `Asc()` function required to evaluate character codes like spaces and paragraph marks.