Use Word VBA to Right-Align Numeric Table Cells
Question details
Use VBA to right-align numeric cells in a Word table without changing nonnumeric cells.

- Product
- Microsoft Word
- Device & OS
- not provided
- Scenario
- A user wants to run a macro on a Word table containing both numeric and nonnumeric values.
- Observed behavior
- Numeric cell contents should be right-aligned, while text and other nonnumeric values retain their existing alignment.
Open the document in a version of Word that supports VBA macros and place the cursor inside the table you want to process. If you want to keep the macro in the document, save it as a Word Macro-Enabled Document (.docm).
Run a VBA macro on the selected Word table
Use this macro to check each cell in the current table and right-align cells whose contents pass the VBA IsNumeric test.
Word includes an end-of-cell marker in each cell's range. The macro removes the final paragraph mark and cell marker from the text before testing it; it does not delete them from the document.
IsNumeric checks the cell text using the applicable numeric conventions. Cells containing labels or other nonnumeric characters are left unchanged.
In Word, open the Developer tab and select Visual Basic. If the Developer tab is hidden, enable it under File > Options > Customize Ribbon.
In the VBA editor, select the document's project, choose Insert > Module, and paste this code: Sub RightAlignNumericCells() Dim cell As Cell Dim cellText As String If Not Selection.Information(wdWithInTable) Then MsgBox "Place the cursor inside a table first." Exit Sub End If For Each cell In Selection.Tables(1).Range.Cells cellText = cell.Range.Text cellText = Trim$(Left$(cellText, Len(cellText) - 2)) If IsNumeric(cellText) Then cell.Range.ParagraphFormat.Alignment = wdAlignParagraphRight End If Next cell End Sub
Return to the document and click inside the table. Select Developer > Macros, choose RightAlignNumericCells, and click Run.
Review the table in Word to confirm that numeric cells are right-aligned and nonnumeric cells retain their previous alignment.

Edit and Align Word Tables in WPS Writer
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. You can manually right-align table cells in WPS Writer; it does not fix or replace the Word VBA macro described above.

Frequently Asked Questions
Why does the macro remove characters before checking a cell?
Word includes a final paragraph mark and an end-of-cell marker in a cell's range text. The macro removes those two characters from the text variable so IsNumeric can test the displayed value.
Does the macro change cells that contain text?
No. It applies right alignment only when IsNumeric returns True for the trimmed cell text. Other cells keep their existing alignment.
What should I do if the macro says the cursor is not in a table?
Click inside the table you want to process, then open Developer > Macros and run RightAlignNumericCells again.
How do I keep the macro in my Word document?
Use File > Save As and select Word Macro-Enabled Document (.docm). A standard .docx file cannot store VBA macros.
Will every number-looking value pass IsNumeric?
Not necessarily. IsNumeric depends on the cell's text and applicable numeric conventions. Values with extra labels or unsupported formatting may be treated as nonnumeric and left unchanged.




