+ Reply to Thread
Results 1 to 14 of 14

Add 0 to start of number if length is less than 5

  1. #1
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Add 0 to start of number if length is less than 5

    Hi all,

    I've attached a workbook with one sheet on it called "Survey Location Table".

    I am looking to add a 0 to front of any number, where the length of the value in an individual cell is less than 5 digits.

    The cells in question will be from Column D onwards.

    So where a number is 8774, as shown in Cell E10 then this should become 08774.

    This still applies if a number is 3 digits long, like shown in Cell E70 then this should become 0901.

    IF numbers are 5 digits long then nothing is to happen with them.


    Any one have any ideas?

    Many thanks,
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Add 0 to start of number if length is less than 5

    Set the number format of the relevant columns to be Custom and "00000"

    oh hold on, so if its 3 chars then it stillonly gets 1 zero appended?
    If someone has helped you then please add to their Reputation

  3. #3
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    3,046

    Re: Add 0 to start of number if length is less than 5

    Custom number format based on lenght?
    Please Login or Register  to view this content.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,437

    Re: Add 0 to start of number if length is less than 5

    To borrow from Lois Lowry's "The Giver", can we be very precise in our language? When you talk about "adding a 0 character to the left of a number" exactly what are you wanting to happen -- just display the number with those insignificant zeroes while the underlying value stays the same, or do you need to convert the number to a text string with those zeroes as part of the cell value? I notice that all of the proposed solutions are changing the number formatting so that the number 901 looks like 0901, but the cell's value is still 901. Do you need the cell value to stay 901, or do you need the cell's value to become the text string "0901"?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Add 0 to start of number if length is less than 5

    Apologies for my illiteracy.

    I would be looking to add a 0 to the front of the string, so as per your example 901 become 0901.

    Any value with a string length of less that 5 requires a 0 to be displayed at the front of the value.

  6. #6
    Valued Forum Contributor
    Join Date
    10-12-2021
    Location
    CT
    MS-Off Ver
    365
    Posts
    462

    Re: Add 0 to start of number if length is less than 5

    @Chriseelis, MrShorty wasn't calling you illiterate; Your question makes sense - that's just his signature.

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,437

    Re: Add 0 to start of number if length is less than 5

    When the number 901 becomes the text string "0901" what will then happen? We frequently encounter issues with numbers stored as text. Most of the time, the solution involves storing numbers as numbers. Converting numbers to text shouldn't be overly difficult, but it could be valuable to think through exactly how you are going to work with these "numbers stored as text" and otherwise how you are interacting with the spreadsheet to be sure that storing numbers as text is the best solution.

  8. #8
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Add 0 to start of number if length is less than 5

    Ahhhhhhhh I see Apologies

  9. #9
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Add 0 to start of number if length is less than 5

    Hi MrShorty.

    So the numbers in this table are then used for a lookup for data from elsewhere. Basically the data from elsewhere shows the numbers as 0901 or 01901, whereas the data that is shown in the attached file shows the numbers as 901 or 1901 etc.

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,437

    Re: Add 0 to start of number if length is less than 5

    You say that the data from elsewhere "shows" numbers as 0901 or 01901. Does this mean that the numbers are stored as text in this other location, or are they the number 901 formatted as "0000" or 1901 formatted as "00000"? Or are these data from elsewhere not even in a spreadsheet? How are you performing the lookup operation (Excel lookup function or something else)? Assuming the "data from elsewhere" is a number stored as text, would it be preferred to convert that to a real number? It often doesn't matter whether you search for numbers stored as numbers or numbers stored as text as long as you are consistent, which often leads to a decision as a programmer whether you will find it easier to consistently store everything as text or consistently store everything as number.

  11. #11
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Add 0 to start of number if length is less than 5

    The data from elsewhere contains numbers that are stored as text.

    The look up is via VBA where it looks for a match of 3 values between the 2 sheets.

  12. #12
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,656

    Re: Add 0 to start of number if length is less than 5

    Try this:
    PHP Code: 
    Option Explicit
    Sub test
    ()
    Dim lr&, lc&, LCell As Range
    Application
    .ScreenUpdating False
    lr 
    Cells(Rows.Count"A").End(xlUp).Row
    lc 
    Cells(1Columns.Count).End(xlToLeft).Column
    Set LCell 
    Cells(lrlc)
    With Range("D2"LCell)
        .
    NumberFormat "@"
        
    .Value Evaluate("=IF(LEN(D2:" LCell.Address ")<5,IF(LEFT(D2:" LCell.Address ",1)<>""0"",""0""&D2:" LCell.Address ",D2:" LCell.Address "),D2:" LCell.Address ")")
        .
    Replace 0""xlWhole
    End With
    Application
    .ScreenUpdating True
    End Sub 
    Quang PT

  13. #13
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    14,958

    Re: Add 0 to start of number if length is less than 5

    Please Login or Register  to view this content.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  14. #14
    Forum Contributor
    Join Date
    03-04-2014
    Location
    Birmingham, England
    MS-Off Ver
    Excel 2019
    Posts
    758

    Re: Add 0 to start of number if length is less than 5

    Quote Originally Posted by bebo021999 View Post
    Try this:
    PHP Code: 
    Option Explicit
    Sub test
    ()
    Dim lr&, lc&, LCell As Range
    Application
    .ScreenUpdating False
    lr 
    Cells(Rows.Count"A").End(xlUp).Row
    lc 
    Cells(1Columns.Count).End(xlToLeft).Column
    Set LCell 
    Cells(lrlc)
    With Range("D2"LCell)
        .
    NumberFormat "@"
        
    .Value Evaluate("=IF(LEN(D2:" LCell.Address ")<5,IF(LEFT(D2:" LCell.Address ",1)<>""0"",""0""&D2:" LCell.Address ",D2:" LCell.Address "),D2:" LCell.Address ")")
        .
    Replace 0""xlWhole
    End With
    Application
    .ScreenUpdating True
    End Sub 
    Fantastic, thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] How do i find smallest and largest number that start with specific number?
    By Ariful Islam in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-29-2021, 09:17 PM
  2. Replies: 2
    Last Post: 08-28-2021, 01:06 AM
  3. Length (count) of sequences with start and end condition
    By jake_jennings1 in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-11-2020, 03:54 AM
  4. Use start and stop times (or length) to fill cells in between for schedules
    By soccer4ard in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-30-2016, 03:08 PM
  5. Calculating the length, start, and end dates of a series of events
    By CharlieDog in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-03-2014, 04:53 PM
  6. [SOLVED] Counting Partial Number Instances Within a Set Number Length.
    By MurasakiK in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-23-2013, 01:56 PM
  7. defining a years length(start and end dates)
    By wheefus in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-13-2008, 11:44 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1