<span style="color: #008000;">\'*******************************************************************************</span>
<span style="color: #0000ff;">Public</span> <span style="color: #0000ff;">Function</span> PadNumber(<span style="color: #0000ff;">ByVal</span> intCurrentNum, <span style="color: #0000ff;">ByVal</span> intMaxNumInList)
<span style="color: #008000;">\'*******************************************************************************</span>
<span style="color: #008000;">\'Description : Pads a number with zeroes on the left, according to the expected</span>
<span style="color: #008000;">\' maximum length of the numbers in the list.</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'Purpose : To keep a number list sorted properly, as with a file list (001,</span>
<span style="color: #008000;">\' 002,..., 010, and not 1, 10, 11,..., 2, 20).</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'Arguments : intCurrentNum (the current number to be padded)</span>
<span style="color: #008000;">\' intMaxNumInList (the top number in the list)</span>
<span style="color: #008000;">\' Note: The arguments are always taken in absolute values</span>
<span style="color: #008000;">\'Returns : The padded intCurrentNum (for example, If 1 and 9999 are sent to</span>
<span style="color: #008000;">\' the function, the result will be 0001)</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'Developed By : Meir Bar-Tal</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'Date : 11-Jan-2007</span>
<span style="color: #008000;">\'</span>
<span style="color: #008000;">\'*******************************************************************************</span>
<span style="color: #008000;">\'Validates the arguments - if invalid then it returns the value as is</span>
<span style="color: #0000ff;">If</span> (<span style="color: #0000ff;">Not</span> IsNumeric(intCurrentNum) <span style="color: #0000ff;">Or</span> <span style="color: #0000ff;">Not</span> IsNumeric(intMaxNumInList)) <span style="color: #0000ff;">Then</span>
PadNumber = intCurrentNum
<span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
<span style="color: #0000ff;">If</span> (Abs(intCurrentNum) >= Abs(intMaxNumInList)) <span style="color: #0000ff;">Then</span>
PadNumber = intCurrentNum
<span style="color: #0000ff;">Exit</span> <span style="color: #0000ff;">Function</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">If</span>
PadNumber = <span style="color: #0000ff;">String</span>(len(<span style="color: #0000ff;">CStr</span>(Abs(intMaxNumInList)))-len(<span style="color: #0000ff;">CStr</span>(Abs(intCurrentNum))), <span style="color: #006080;">"0"</span>) _
& <span style="color: #0000ff;">CStr</span>(Abs(intCurrentNum))
<span style="color: #008000;">\'*******************************************************************************</span>
<span style="color: #0000ff;">End</span> <span style="color: #0000ff;">Function</span>
<span style="color: #008000;">\'*******************************************************************************</span>
Usage examples:
Msgbox PadNumber(4, 34567) <span style="color: #008000;">\'Returns 00004</span>
Msgbox PadNumber(-4, 34567) <span style="color: #008000;">\'Returns 00004 </span>
Msgbox PadNumber(4, -34567) <span style="color: #008000;">\'Returns 00004</span>
Msgbox PadNumber(34567, 4) <span style="color: #008000;">\'Returns 34567</span>
Msgbox PadNumber(4, 9) <span style="color: #008000;">\'Returns 4</span>
Msgbox PadNumber(<span style="color: #006080;">"Hello"</span>, 9999) <span style="color: #008000;">\'Returns Hello</span>
You’re invited to customize this function according to your needs.



Recent Comments