When I’m working with VBA in Excel, formatting numbers can be crucial for readability and presentation. That’s where the VBA FORMATNUMBER function comes into play; it’s all about converting a raw number into a more display-friendly format. This handy function categorizes under the data type conversion family, providing a straightforward way to format numerical expressions as strings, ensuring that the data looks exactly how I need it to—whether that means including a certain number of decimal places, prepping numbers for parenthetical display when negative, or grouping digits for better visual comprehension.
Syntax
FormatNumber( Expression, [NumDigitsAfterDecimal], [IncludeLeadingDigit], [UseParensForNegativeNumbers], [GroupDigits] )
Argument | Description | Values |
---|---|---|
Expression | The numeric value I’m formatting. | |
NumDigitsAfterDecimal | Digits after the decimal point. | Integer, vbUseDefault |
IncludeLeadingDigit | Include a leading digit. | vbTrue, vbFalse, vbUseDefault |
UseParensForNegativeNumbers | Negative numbers in parentheses. | vbTrue, vbFalse |
GroupDigits | Group digits with commas. | vbTrue, vbFalse, vbUseDefault |
- vbTrue generally represents a boolean true value.
- vbFalse represents a boolean false value.
- vbUseDefault lets the system use regional settings.
Arguments
When I use the FormatNumber function, my goal is to transform a numeric expression into a formatted string. Here’s the lowdown on the parameters I can tweak:
- Expression: That’s the number I’m trying to shape up.
- NumDigitsAfterDecimal (optional): Decides how many decimal places to show. Skip it, and it’s -1 by default, tailoring the decimals based on regional settings.
- IncludeLeadingDigit:
- vbFalse: No zero before the dot on numbers less than one.
- vbTrue: Slap on a leading zero.
- vbUseDefault: Let the system decide.
I get to choose whether to group digits or not with GroupDigits, using the local delimiter:
- vbFalse: Leaves numbers ungrouped.
- vbTrue: Adds those helpful separators between thousands and so on.
- vbUseDefault: Follows the computer’s own rules.
Negative numbers get their own style, too, with UseParensForNegativeNumbers:
- vbFalse: Just a simple minus sign.
- vbTrue: Wraps them up nicely in parentheses.
- vbUseDefault: Trusts the computer’s usual way of doing things.
And that’s about it. These arguments let me convey numbers precisely as needed.
Example
I recently put together a small snippet of VBA that demonstrates how handy the FormatNumber
function can be. Here’s what I did:
Sub example_FORMATNUMBER()
Range("B1").Value = FormatNumber(Range("A1"))
End Sub
In this case, the value in cell A1 gets formatted with the default number format and then shows up in cell B1. I didn’t use FormatCurrency
or FormatPercent
this time, but they work in a similar manner. Also, I didn’t need Debug.Print
here, as the result is directly visible in the spreadsheet. It’s a simple yet effective way to ensure numbers look exactly how you need them.
Notes
- Error Handling: I ensure the value I’m formatting is numeric to avoid a run-time 13 error.
- Data Type: I remember the formatted expression is a string.
- Regional Settings: My computer’s settings can affect number formats.
- Default Formats: If I don’t specify a format, VBA uses the default.
- Custom Formats: I can use custom patterns with commas, percentages, or currency symbols.
- Functions:
FormatNumber
,FormatCurrency
, andFormatPercent
are my go-tos. - Arguments: I use parentheses to include optional arguments, like the number of decimal places.