logo
search
VBA & Macro Problems

How to Remove Numbers and Punctuation from Word Table Cells Using VBA

Bushra ParveenBushra Parveen Sep 28, 2026 871 views

Question details

The user needs a Word VBA macro to iterate through table cells and remove specific digits and English punctuation marks without affecting the valid cell-ending characters.

How to Remove Numbers and Punctuation from Word Table Cells Using VBA
Product
Word
Device & OS
not provided
Scenario
Cleaning up data within Word tables by stripping out unwanted numbers and punctuation marks using automation.
Observed behavior
Currently, the table cells contain mixed text, numbers, and punctuation. The goal is to retain only the text by automatically stripping out the digits and selected punctuation.
Before you start

Verify whether the numbers in your tables are typed text or generated by an automatic numbered list format, as macros process typed text differently than list formatting.

Solution 1Recommended

Use a Regular Expression VBA Macro to Clean Table Cells

Utilize a Regular Expression (RegEx) within a VBA macro to efficiently target and remove digits and specific punctuation across all table cells in the active document.

Using Regular Expressions in VBA allows you to define exactly which characters to keep and which to remove. When working with Word tables, it is crucial to carefully strip the hidden end-of-cell markers before processing the text to avoid corrupting the table structure.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications (VBA) editor.

2
Enable Regular Expressions

In the VBA editor, click on 'Tools' in the top menu, select 'References', check the box for 'Microsoft VBScript Regular Expressions 5.5', and click 'OK'.

3
Insert a New Module

Click 'Insert' from the top menu and select 'Module' to create a blank workspace for your macro code.

4
Write the Macro Code

Write a script using a 'For Each' loop to iterate through 'ActiveDocument.Tables' and then through each 'Cell' in 'Table.Range.Cells'. Extract the text using 'Left(Cell.Range.Text, Len(Cell.Range.Text) - 2)' to exclude the cell markers.

5
Apply RegEx and Run

Initialize your RegEx object, set the pattern to target digits (\d) and unwanted punctuation, and replace them with an empty string. Write the cleaned text back to 'Cell.Range.Text' and press F5 to run the macro.

Use a Regular Expression VBA Macro to Clean Table Cells
Handling Numbered Lists: If the numbers in your table are generated by automatic numbered lists rather than typed text, this macro will not remove them. You must convert numbered lists to plain text before running the script.
Advanced Automation with WPS

Automate Table Cleanup with WPS Writer

WPS Office provides comprehensive support for VBA macros. You can seamlessly run your custom VBA scripts to remove numbers and punctuation from complex tables directly within WPS Writer.

  1. 1. Open Your Document: Launch WPS Writer and open the document containing the tables you want to clean.
  2. 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon menu to access advanced tools.
  3. 3. Open the Macro Editor: Click the 'Macros' button, type a name for your new macro, and click 'Create' to open the WPS VBA Editor.
  4. 4. Insert Your Code: Paste your regular expression and table-looping VBA code into the module window.
  5. 5. Execute the Cleanup: Click the 'Run' button (or press F5) to execute the macro and instantly strip the numbers and punctuation from your tables.
Fully compatible with Microsoft Word VBA macrosProcess large tables quickly with minimal resource consumptionSupports Microsoft Word file formats including .docm and .docxFree and lightweight alternative to Microsoft Office
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro delete the table structure or cause an error?

Word table cells contain hidden end-of-cell markers (usually represented by ASCII characters 13 and 7). If your macro replaces the entire cell contents including these markers, it can corrupt the table. Always extract the text excluding the last two characters before processing.

How can I modify the macro to only delete specific punctuation marks?

You can adjust your Regular Expression pattern. For example, to only remove commas and periods, use a pattern like '[,.]' instead of a broad punctuation class, ensuring only those exact characters are replaced with empty strings.

Will this macro work if I have merged cells in my Word table?

Iterating through rows and columns numerically can cause errors if the table contains merged cells. To avoid this, write your loop to iterate directly through the 'Table.Range.Cells' collection, which handles merged and split cells properly.