VBA & Macro Problems

How to Format VBA Numbers with Leading Zeros and Edit Table Formulas in WPS Office Spreadsheet

Huda QurayshiHuda Qurayshi Nov 13, 2026 869 views

Question details

The user needs to format VBA-generated sequence numbers with consistent four-digit leading zeros and edit calculated-column formulas in an Excel table after rows are populated.

Product
WPS Office Spreadsheet
Device & OS
not provided
Scenario
The user is generating a file with VBA sequence numbers from data in an Excel table. The table uses calculated-column formulas that are not visible until the table has populated rows.
Observed behavior
The VBA-generated sequence numbers appear inconsistently as 001, 002, 0100, and 0101 instead of consistent four-digit values. The Excel table calculated-column formula is not visible until the table has populated rows.
Before you start

Open the workbook in WPS Office Spreadsheet and enable macros so the VBA code can run. Confirm that the source data is in an Excel table and locate the calculated column you need to edit.

Solution 1Recommended

Use the VBA Format Function to Apply Consistent Four-Digit Leading Zeros

Use this when VBA-generated sequence numbers show inconsistent lengths because conditional zero-padding does not cover all values.

The Format function converts the numeric value to text with a fixed pattern. In this case, Format(D, "0000") always produces four characters, so 1 becomes 0001, 10 becomes 0010, and 100 becomes 0100.

1
Open the VBA editor

In WPS Office Spreadsheet, press Alt+F11 to open the VBA editor and find the module containing the sequence number code.

2
Replace conditional code with Format

Delete the If D < 10 Then ... Else ... End If block and enter array192(Z) = x.Offset(0, -1) & Format(D, "0000").

3
Run the macro

Switch back to WPS Office Spreadsheet and run the macro from the Macro dialog or with the assigned shortcut to regenerate the sequence numbers.

Consistent four-digit output: With Format(D, "0000"), values 1, 10, and 100 are written as 0001, 0010, and 0100.
Recommended WPS solution

Use WPS Office Spreadsheet to Format VBA Number Sequences and Edit Table Formulas

WPS Office Spreadsheet supports VBA macros and Excel table calculated columns. Use the Format(D, "0000") replacement directly in the VBA editor, then edit table formulas after adding data rows.

  1. 1. Open the workbook in WPS Office Spreadsheet: Launch WPS Office Spreadsheet and open the workbook that contains the VBA macro and Excel table.
  2. 2. Edit the VBA sequence number code: Press Alt+F11 to open the VBA editor, find the conditional zero-padding block, and replace it with array192(Z) = x.Offset(0, -1) & Format(D, "0000").
  3. 3. Run the macro: Return to the spreadsheet and run the macro from the Developer tab or Macro dialog to regenerate the sequence numbers.
  4. 4. Correct the table calculated-column formula: Add a populated data row, select the calculated column cell, enter the corrected formula in the formula bar, and confirm the calculated column overwrite.
  5. 5. Save as an Excel-compatible workbook: Go to Menu > Save As and choose the Excel .xlsx or macro-enabled format to keep the file compatible with Microsoft Excel.
Apply consistent four-digit leading zeros with the WPS VBA Format functionEdit Excel table calculated-column formulas without switching office suitesOpen and save Excel .xlsx and macro-enabled workbooks with format compatibilityFree, lightweight Office suite with a familiar ribbon interface
microsoft office alternative - wps office

Frequently Asked Questions

Why do my VBA sequence numbers lose leading zeros for values above 99?

Conditional code such as If D < 10 Then ... Else ... End If only adds zeros for specific ranges. Replace it with array192(Z) = x.Offset(0, -1) & Format(D, "0000") in the VBA editor to force four digits for all values.

How do I apply the Format function to multiple sequence numbers in VBA?

Open the VBA editor with Alt+F11, locate the sequence number generation block, and replace the conditional zero-padding logic with Format(D, "0000") so every value uses the same four-character pattern.

Why can’t I see an Excel table calculated-column formula?

An Excel table must have at least one populated data row before the calculated-column formula becomes visible. Add a value to a data row, then select the calculated column cell to view or edit the formula in the formula bar.

How do I edit a calculated-column formula after the table has rows?

Select the calculated column cell in a populated row, enter the corrected formula in the formula bar, press Enter, and confirm the overwrite prompt so the formula copies throughout the calculated column.

Can WPS Office open and run VBA macro workbooks created in Microsoft Excel?

Yes, WPS Office Spreadsheet can open Excel macro-enabled workbooks and run many VBA macros. Open the workbook in WPS Office Spreadsheet and enable macros if prompted.