VBA & Macro Problems

Use Word VBA to Right-Align Numeric Table Cells

Khadija KhanKhadija Khan Nov 22, 2026 868 views

Question details

Use VBA to right-align numeric cells in a Word table without changing nonnumeric cells.

Use Word VBA to Right-Align Numeric Table 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.
Before you start

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

Solution 1Recommended

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.

1
Open the VBA editor

In Word, open the Developer tab and select Visual Basic. If the Developer tab is hidden, enable it under File > Options > Customize Ribbon.

2
Insert the macro

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

3
Run the macro

Return to the document and click inside the table. Select Developer > Macros, choose RightAlignNumericCells, and click Run.

4
Check the table

Review the table in Word to confirm that numeric cells are right-aligned and nonnumeric cells retain their previous alignment.

Run a VBA macro on the selected Word table
Macro permissions: Word may block macros depending on your security settings or your organization's policy. Enable macros only when you trust the document and its code.
Free Microsoft Office alternative

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.

Manually adjust paragraph alignment in table cellsOpen and edit Microsoft Word DOC and DOCX filesUse a familiar, easy-to-navigate document interfaceMove existing documents into a lightweight office suite
microsoft office alternative - wps office

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.