Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

City Short Courses

Formatting Numbers
To format numbers in cells, use the NumberFormat property of an Excel Range object.

It works the same way as Excel number formatting for


worksheet data. The display in the cell changes while the
underlying number (see Formula Bar) remains the same.

The Visual Basic Format function can be used in more general situations away from cells.

NumberFormat property
The NumberFormat property takes a string argument that contains placeholders for the
numbers, which can be of two types.
 The 0 (zero) placeholder displays insignificant zeros. E.g., if you want "8.9" to be
displayed as "8.90", use the format #.00
 The # placeholder does not display extra zeros: e.g., if the format is #.##, and you type
"8.90" in the cell, "8.9" is displayed.

Examples
Selection.NumberFormat = "$#,##0.00" Displays currency symbol,
commas are thousands
separators, and 2 decimal
places
Selection.NumberFormat = "$#,##0" As above, with no decimal
places
Selection.NumberFormat = "[$$-409]#,##0.00" US dollars (as recorded)
Selection.NumberFormat = "[$€-2] #,##0.00" Euro symbol, as recorded
Selection.NumberFormat = "@" Number appears as typed,
e.g. phone numbers like
0800…

Format function
The Format function is available in all Visual Basic environments. It is useful for message boxes:

Range("A1") = 5689
MsgBox Format(Range("A1"), "£#,##0.00") 'displays "£5,689.00"

More typically it is used for formatting variables: here a number


variable is declared, a value assigned to it and then the Format
function is used to control the display.

Dim num as Double


num = 5689
MsgBox Format(num,"£#,###.00")

Reference
A range of formatting options can be found at:
https://msdn.microsoft.com/en-us/library/office/gg251755.aspx

You might also like