How to Remove Numbers and Punctuation from Word Table Cells Using VBA
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.

- 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.
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.
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.
Press Alt + F11 on your keyboard to open the Microsoft Visual Basic for Applications (VBA) editor.
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'.
Click 'Insert' from the top menu and select 'Module' to create a blank workspace for your 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.
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.

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. Open Your Document: Launch WPS Writer and open the document containing the tables you want to clean.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon menu to access advanced tools.
- 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. Insert Your Code: Paste your regular expression and table-looping VBA code into the module window.
- 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.

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.




