Overview
In Excel, changing the “Display Format” (formatting) is often necessary to improve the appearance of numbers. In particular, the thousands separator (comma every 3 digits) is essential for readability when dealing with amounts or quantities.
This article introduces how to programmatically apply arbitrary display formats to cells using VBA. It is recommended for those who want to automate tasks or standardize templates.
Sample Code
Sub FormatCellAsThousands()
Range("D3").NumberFormatLocal = "#,##0"
End Sub
Code Explanation
- Range(“D3”)Specifies the target cell. You can change this to any cell or range.
- NumberFormatLocalSets the display format using the local language settings. In a standard environment, #,##0 formats the number as an integer with commas.
Meaning of “#,##0”
- #: Displays a digit if necessary (suppresses leading zeros).
- ,: Inserts a comma separator every 3 digits.
- 0: Displays a zero if the number is zero (ensures at least one digit is shown).
- Example: 1234567 -> 1,234,567
Examples of Advanced Formatting
| Format Code | Display Example | Description |
| #,##0 | 1,000 | Integer with commas |
| ¥#,##0 | ¥1,000 | Amount with Yen symbol |
| $#,##0 | $1,000 | Amount with Dollar symbol |
| #,##0.00 | 1,000.00 | Number with two decimal places |
| yyyy/mm/dd | 2025/08/07 | Date format |
| hh:mm:ss | 14:30:00 | Time format |
Important Notes
- Localization:
.NumberFormatLocalapplies formats based on the user’s regional settings (e.g., Japanese). - Global Compatibility: If you need the macro to work in English environments or globally, it is recommended to use
.NumberFormatinstead (e.g.,Range("D3").NumberFormat = "#,##0"). - Resetting: If necessary, you can add code to clear existing formats before applying new ones.
Applications
- Batch Processing: A macro that formats all monetary data at once.
- Report Generation: Standardizing the layout before printing invoices or reports.
- Event Handling: Automatically applying separators immediately after data entry (using
Worksheet_Change).
Summary
This article explained how to set arbitrary display formats for Excel cells using VBA.
By using NumberFormatLocal, you can output or print data with a professional appearance, leading to improved work efficiency. Please try incorporating this method if you want to automatically create clean and organized tables in your daily tasks.
