Add leading zeroes in Function

hyoung3

New Member
Joined
May 9, 2011
Messages
21
Hi, I am trying to format a number to add leading zeroes in a function I have created. The input Integer will have a maximum length of 5 characters, but the output must be five characters with the proper number of leading zeroes. I tried using the format function (ie. Format(Strike, "00000"), but the output only gives me the input strike. Here is an example:

Code:
Public Function OSI(Strike As String)    
    'Declare variable to format Dollar Strike
    Dim IntDollar As Single
    Dim aDollar As Single
        
    'Dollar Strike format
        IntDollar = Int(Strike)
        aDollar = Format(IntDollar, 00000)
             
    'Output
        OSI = aDollar
    
End Function
If the Strike input equals 25.5 then the OSI output should be 00025. With this I am only getting 25.

I appreciate the help!
 

njimack

Well-known Member
Joined
Jun 17, 2005
Messages
7,761
Most importantly, you need quotes around the 2nd argument of the Format function. Nearly as importantly, you've got several redundant steps...

Code:
Public Function OSI(Strike As String)
       
    OSI = Format(Int(Strike), "00000")
    
End Function
 

Mohammad Basem

Well-known Member
Joined
Dec 24, 2011
Messages
1,218
Try,
Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Dim aDollar As String[/COLOR][/SIZE][/FONT]
instead of
Code:
[FONT="Consolas"][SIZE="2"][COLOR="Navy"]Dim aDollar As Single[/COLOR][/SIZE][/FONT]
 

hyoung3

New Member
Joined
May 9, 2011
Messages
21
Thanks for the help all! I got it to work!
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,886
Why do you need a UDF? This can be done with standard Excel functions:

=TEXT(INT(A1),"00000")
 

Forum statistics

Threads
1,082,384
Messages
5,365,144
Members
400,825
Latest member
Sreekanth_21

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top