+ Reply to Thread
Results 1 to 5 of 5

Thread: Insert leading zeros only when matching certain requierments

  1. #1
    Registered User
    Join Date
    09-02-2011
    Location
    Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    21

    Lightbulb Insert leading zeros only when matching certain requierments

    Hi all I'm new to this forum.
    I tried to search for answer but nothing found....

    i have to make reports every day and i receive raw timing data like in attachment screenshot raw-data.jpg

    and i am trying to add double zero only to cells which start with :

    also i want to add single leading zero to cells which which start with hours 1 to 9

    so basically the transformed data should look like this screenshot


    can you please help me find formula to do this transformation?
    Attached Images Attached Images
    Last edited by godlev; 09-02-2011 at 05:21 AM. Reason: solved

  2. #2
    Registered User
    Join Date
    04-18-2011
    Location
    Surrey, UK
    MS-Off Ver
    Excel 2003
    Posts
    28

    Re: Insert leading zeros only when matching certain requierments

    Hi godlev,

    I think you could use the FIND formula with a few IF statements here to do this.

    If your data is in cell A1, you could enter a formula in cell B1 that says:

    =IF(FIND(":",A1)=1,"00" & A1,IF(FIND(":",A1)=2,"0" & A1,A1))

    This is saying:

    If the position of a colon ( : ) is the first character in the string, then it means you need to add a "00". Otherwise if the colon is the second character in the string, you need to add a single "0". Otherwise, the colon is not the first or second character, which must mean that you start with a two digit value (like "12" or whatever), so just return whatever is in A1.

    This will mean that when you copy the formula down, you will have a list of formatted values (which you could paste over the original data if you wanted).

    If you wanted to write over the data "in-situ", you would need to use it doing visual basic macros, which is possible, but a step up from formulas. In this case you could make it so that you can the range of cells that you are trying to update, and the macro will scan each cell in the selection and see if it needs to be modified.

    Anyway, hope this helps.

    Paul

  3. #3
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Insert leading zeros only when matching certain requierments

    Try this
    =IF(ISNUMBER(A1),A1,IF(LEN(TRIM(A1))=6,"00"&TRIM(A1),"00"&TRIM(A1)&"0"))*1
    Format the cells "hh:mm:ss"

    This will allow for Excel possibly automatically recognising say 1:00:00 or 11:00:00 as a time.
    It should also handle examples like :00:4 should they occur.

    [EDIT]
    This extention of Pauls' suggestion might be better
    =IF(OR(A1="",ISNUMBER(A1)),A1,IF(FIND(":",A1)=1,"00" & A1,IF(FIND(":",A1)=2,"0" & A1,A1))*1)
    This will handle numeric cells and blanks should they occur.

    One thing to note my first suggestion will convert :00:4 to 00:00:40,
    Pauls' formula will return 00:00:04
    Last edited by Marcol; 09-02-2011 at 05:25 AM.
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  4. #4
    Registered User
    Join Date
    09-02-2011
    Location
    Bulgaria
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Insert leading zeros only when matching certain requierments

    @PaulG2011 - BIG thank you for the swift reply - your formula works PERFECT!

    @Marcol - thank you for your reply - your formula inserts single zero in front of the :

    Case SOLVED!

  5. #5
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Insert leading zeros only when matching certain requierments

    The formule I offered, (in both cases), should convert the string to a number.
    If the cells are formatted "hh:mm:ss" then all should work.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0