Google Search Results
You arrived here after searching for the following phrases:
Click a phrase to jump to the first occurrence, or return to the search results.
Sometimes a numbered list is required, such as a list to populate a ListView, TreeView or one to create sequential files in a folder. One of the main problems is that such lists typically sort the items lexicographically, so that numbered items end-up in the wrong order, such as File_1, File_10, FIle_11, …, File_2, FIle_20, etc.
The following function transforms the required number to a string with padding zeroes on the left. This is accomplished by taking the difference between the string lengths of the maximum value in the list and the given number.
'*******************************************************************************
Public Function PadNumber(ByVal intCurrentNum, ByVal intMaxNumInList)
'*******************************************************************************
'Description : Pads a number with zeroes on the left, according to the expected
' maximum length of the numbers in the list.
'
'Purpose : To keep a number list sorted properly, as with a file list (001,
' 002,…, 010, and not 1, 10, 11,…, 2, 20).
'
'Arguments : intCurrentNum (the current number to be padded)
' intMaxNumInList (the top number in the list)
' Note: The arguments are always taken in absolute values
'Returns : The padded intCurrentNum (for example, If 1 and 9999 are sent to
' the function, the result will be 0001)
'
'Developed By : Meir Bar-Tal
'
'Date : 11-Jan-2007
'
'*******************************************************************************
'Validates the arguments - if invalid then it returns the value as is
If (Not IsNumeric(intCurrentNum) Or Not IsNumeric(intMaxNumInList)) Then
PadNumber = intCurrentNum
Exit Function
End If
If (Abs(intCurrentNum) >= Abs(intMaxNumInList)) Then
PadNumber = intCurrentNum
Exit Function
End If
PadNumber = String(len(CStr(Abs(intMaxNumInList)))-len(CStr(Abs(intCurrentNum))), "0″) _
& CStr(Abs(intCurrentNum))
'*******************************************************************************
End Function
'*******************************************************************************
Usage examples:
Msgbox PadNumber(4, 34567) 'Returns 00004
Msgbox PadNumber(-4, 34567) 'Returns 00004
Msgbox PadNumber(4, -34567) 'Returns 00004
Msgbox PadNumber(34567, 4) 'Returns 34567
Msgbox PadNumber(4, 9) 'Returns 4
Msgbox PadNumber("Hello", 9999) 'Returns Hello
You’re invited to customize this function according to your needs.
Posted in Meir Bar-Tal's Blog, VBScript


Meir Bar-Tal




May 5th, 2008 at 5:22 am
Thanks for the function Meir.
Small correction in the examples, I think you’re missing a zero, eg.
Msgbox PadNumber(4, 34567) ‘Returns 0004
should be
Msgbox PadNumber(4, 34567) ‘Returns 00004
May 5th, 2008 at 7:06 pm
You’re right.
Thanks for the correction, I’ve amended it.
December 19th, 2008 at 10:04 pm
Padding a number using the Right function:
PadNumber = Right(’0000′ & Number, 5)
If the Number is 123, PadNumber will be the rightmost five characters of 0000123 which is 00123.
December 19th, 2008 at 10:51 pm
That is true, but what’s your point? The point of my function is that you just need to give the maximum number in the list and it calculates the number of zeroes that are required for the padding, regardless of the number size. So, it’s truly generic. Your example uses a hard-coded string of zeroes. In short, what did you want to prove?