Pad single-digit numbers with a zero Pad single-digit numbers with a zero vba vba

Pad single-digit numbers with a zero


VBA has a Format() function that you can use to pad numbers.

animal = "sinani-" & Format$(i, "00")

This will pad single-digit numbers with a 0. Your two- and three-digit numbers will continue to work as expected.


In the fifth line use the Format function like this:

animal = "sinani-" & Format(i, "#00")

The # means optionally a digit (i.e. present only if there are that many digits in i), 0 means definitely a digit, whereby leading zeros are used if i hasn't got enough digits.


Concatenate with a leading series of zeroes and peel off as many digits from the right-hand side as you need.

animal = "sinani-" & Right("00" & i, 2)'alternate for many leading zeroes (e.g. DUNS number)animal = "sinani-" & Right(String(9, "0") & i, 9)