Formatting a number to two digits even if the first is a 0 vba Formatting a number to two digits even if the first is a 0 vba vba vba

Formatting a number to two digits even if the first is a 0 vba


You cannot format an integer variable, you need to use a string variable for formatting.

You can convert the day part of a date to a format with leading zeros using the Day function to extract the day number from the date, and then using the Format function with a "00" format to add a leading zero where necessary

Format(Day(myDate), "00")

myDate is a Date variable containing the full Date value

The following macro can be used as a working sample

Sub Macro1()    Dim myDate As Date    myDate = "2015-5-1"    Dim dayPart  As String    dayPart = Format(Day(myDate), "00")    MsgBox dayPartEnd Sub


Sure you can format an integer, you just convert it to string within the format command:

formattedIntAsString = Format(Cstr(intValue), "00")


I did it like this:

number_item = 2number_item = WorksheetFunction.Text(number_item, "00") 

This will do the job.