How to Convert Excel Values to 13-Digit Numbers with a VBA Macro
Question details
The user needs an automated VBA macro to display existing numeric values in column N as 13-digit strings with leading zeros, specifically without using helper columns or spreadsheet formulas.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automatically formatting a designated range of cells to a fixed 13-digit length by appending leading zeros through a script.
- Observed behavior
- By default, the spreadsheet strips leading zeros from numeric values, requiring a programmed solution to pad the existing numbers up to a 13-character length.
Before running a macro to convert numbers to text, save a backup copy of your workbook. Forcing numbers to text format will change how they behave in future mathematical calculations.
Use a VBA Macro to Convert Values to Text with Leading Zeros
This automated VBA solution searches for the last used row in the target column and explicitly converts each numeric value into a 13-digit text string by adding an apostrophe.
This method is highly recommended when you need the leading zeros to be permanently stored as text rather than just visually formatted.
The addition of the apostrophe character prefix tells the spreadsheet software to strictly interpret the 13-digit string as text, preventing the automatic removal of the zeros.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the top menu, click on 'Insert' and select 'Module' to create a blank workspace for your macro code.
Paste a VBA script that loops through column N starting at row 6. Use the logic: Format(cell.Value, "0000000000000") and prepend an apostrophe (') to the formatted result.
Close the editor, return to your worksheet, and press Alt + F8. Select your new macro and click 'Run' to process the range.

Apply a Custom Number Format Without Changing Data Types
If you require the underlying cell values to remain pure numbers for mathematical formulas, apply a custom 13-digit number format instead of converting them to text.
Easily Run VBA Macros and Format Data with WPS Spreadsheet
WPS Spreadsheet features excellent compatibility with Excel VBA macros. You can effortlessly automate complex data transformations, including 13-digit text conversions and custom number formats, within its robust macro environment.
- 1. Open Your Document: Launch WPS Spreadsheet and open the workbook containing the column you wish to format.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic' or 'Macros' to open the VBA editor.
- 3. Run the Formatting Macro: Insert your formatting script to process the 13-digit values, save the module, and run the macro directly on your active worksheet.

Frequently Asked Questions
Why do leading zeros disappear when I type them into Excel?
By default, spreadsheet software treats any cell containing only digits as a numeric value. It automatically trims leading zeros because they do not alter the mathematical magnitude of the number. To retain them, you must format the cell as text before typing or apply a custom number format.
Will converting numbers to text with a VBA macro break my formulas?
It might. If a VBA macro converts a number to text (by adding an apostrophe prefix), mathematical functions that rely strictly on numeric inputs, such as SUM or AVERAGE, may ignore those cells or generate calculation errors.
How do I dynamically find the last used row in a column using VBA?
You can identify the last used row in a specific column dynamically using the VBA statement: LastRow = Cells(Rows.Count, "N").End(xlUp).Row. This ensures your macro always processes the correct amount of data even as your dataset grows.
Does WPS Office support VBA macros?
Yes, compatible versions of WPS Office fully support VBA macros. Users can seamlessly record, write, and execute scripts to perform automated tasks like custom number formatting exactly as they would in Microsoft Excel.




