Question

How can I add leading zeroes to a number? For example:

Dim stracctnumber as String
stracctnumber = 987654321

If stracctnumber is less than 15 characters, then add leading zeroes to the account number.

The final number should be

stracctnumber = "000000987654321"

Can anyone help me?

Was it helpful?

Solution

stracctnumber = Format(stracctnumber, String(15, "0"))

OTHER TIPS

strAcct = Right("000000000000000" & strAcct, 15)

Note that concatenation is relatively 'expensive'. If this is just for display, rather than modifying the underlying value, consider using the Format() function.

B8 = Format((Format(B2, "###,###")), "@@@@@@@")

This is to add spaces to left.

WORKS GREAT in VBA Excel.....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top