Search notes:

Excel: Range.numberFormat

A range's numberFormat property specifies how the underlying cells' data is formatted.
numberFormat is technically a variant but seems to always be a string that corresponds to the Format Cells dialog (to be opened with right-click on a cell/range or via Ribbon-Home -> Number).

Format of the numberFormat value

The value of numberFormat consists of one to up to four parts or elements which are separated from each other by a semicolon (;):
If only one element is given, it controls all numbers, positive and negative.
In order to use the value of a cell whose data type is text, the at sign (@) must be used.
Some possible values for numberFormat are
General
0000 Display numbers with 4 digits (and leading zeros)
0.00 Specify number of decimal characters to be displayed
0.00;[Red]0.00 Display negative numbers in red
0.00_ ;-0.00
0.00_ ;[Red]-0.00
d/mm/yy;@
h:mm:ss;@
[$-nnn]… Format a date(?) in the language id nnn (see also this Stackoverflow question)
etc.

Formatting dates

A number might be formatted according to ISO 8601 like so
selection.numberFormat = "yyyy-mm-dd""T""hh:mm:ss"
or, without the T that separates the date portion from the time portion:
selection.numberFormat = "yyyy-mm-dd hh:mm:ss"
Note: the mm is interpreted as a two digit month number unless it follows a hh or precedes an ss formatting instruction.
That is: a minute by itself cannot be displayed!

Inserting numbers (especially with leading zeroes) as text

rng.numberFormat = "@" changes the data type of the content of a range to text. This allows to insert text that represent numbers, especially such with leading zeroes, as text, which left-aligns them.
When doing that, Excel still believes this is an error and displays a green triangle in the cell. To turn that triangle off, the ignore property of errors(xlNumberAsText) needs to be set to true.
option explicit

sub main() ' {
 '
 '  Text is converted to number: leading zeroes are removed
 '  and number is right aligned:
 '
    cells(1,1) = "0001"

 '
 '  Text is inserted as number THEN converted to string
 ' (which still removes leading zeroes)
 '
    cells(2,1) = "0002"
    cells(2,1).numberFormat = "@"

 '
 '  Format of cell is changed to text THEN text is inserted.
 '  This keeps leading zeroes but also has green triangle that
 '  indicates an error in the cell
 '
    cells(3,1).numberFormat = "@"
    cells(3,1) = "0003"

 '
 '  Remove green triangle by setting errors(…).ignore
 '  property to true:
 '
    cells(4,1).numberFormat = "@"
    cells(4,1) = "0004"
    cells(4,1).errors(xlNumberAsText).ignore = true

    activeWorkBook.saved = true

end sub ' }
Github repository about-MS-Office-object-model, path: /Excel/Range/numberFormat-insert-number-as-string.bas

Misc

The underscore (_) is used to insert a space into the formatted result.
The underscore needs to be followed by a character whose width determines the width of the space that is inserted.

See also

The keyboard shortcuts ctrl+shift+1 through ctrl+shift+6 apply some predefined formats.
The Excel function cell, especially cell("format", ref).
application.decimalSeparator, .thousandsSeparator and useSystemSeparators.
workbook.deleteNumberFormat

Index