SetNumberFormat function

Applies To
Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel for the web

Use .SetNumberFormat() to attach a preferred number format to the number itself instead of to its cell. The result is a formatted number value. For example, show a portion as 30 g while formulas still see 30.

.SetNumberFormat() is a dot function: you call it on a value by typing a period after the value. It returns a new value and leaves the original unchanged. For an overview, see Data types and dot functions in Excel.

Note

These features are rolling out to Microsoft 365 Insiders in Beta Channel on Windows Version 2612 (Build 20715.20000) or later and Mac Version 16.117 (Build 26111815) or later, and in Excel for the web.

Syntax

.SetNumberFormat() returns a copy of value with the number format you specify, or without its preferred format if you omit number_format.

value.SetNumberFormat([number_format])

Argument Description
value (required) The value before the dot: a number, with or without fields. Text, Booleans, and entities come back unchanged, so you can format a range that mixes numbers with other values. Can be a cell reference, a formula, or a range.
number_format (optional) A number format code, as text. Use the same codes as in Format Cells > Custom. If you omit it, .SetNumberFormat() removes the value's preferred format.

Notes

  • .SetNumberFormat() works the same way as FORMATNUMBER. Use whichever reads better in your formula. The dot form chains after other dot functions.
  • The number doesn't change, and any fields stay. Arithmetic uses the number and drops the format.
  • A number typed before the dot needs parentheses, as in =(12.34).SetNumberFormat("$#,##0.00"), because Excel reads 12.34. as part of the number.
  • Excel shows the preferred format only in cells formatted as General. If a number doesn't show your format, select its cell, and then select Home > Number Format > General.
  • An empty format code ("") makes the value show nothing, but the number is still there.
  • To remove a value's preferred format, omit number_format: =A2.SetNumberFormat(). A number with no fields comes back as a plain number; a number with fields keeps them.

Examples

Copy the example data and paste it in cell A1 of a new Excel worksheet. If you need to, you can adjust the column widths to see all the data.

Example 1

Attach a format from a cell.

Label Value
Training hours 12.5
Format 0.00" hours"
Formula Result
=B2.SetNumberFormat(B3) 12.50 hours
=A5+1 13.5

12.5 shown as 12.50 hours, with 13.5 below it

Example 2

Format a number you type, format a number with fields, and then remove the format.

Formula Result
=(12.34).SetNumberFormat("$#,##0.00") $12.34
=(30).SetFields("Item","Smoky red sauce").SetNumberFormat("0"" g""") 30 g
=A3.Item Smoky red sauce
=A3+0 30
=A3.SetNumberFormat() 30
=A6.Item Smoky red sauce

A6 removes the format and keeps the Item field.

Errors

Error Cause
#VALUE! number_format isn't a format code Excel can read.

See also