Results 1 to 10 of 10

Thread: Leading zeros

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    62

    Leading zeros

    I'm in a pickle so I come seeking the experts!

    I have a range of data that contains cells that report time in hours mins and seconds. If the amount of time is in seconds only, it's reporting as :00 and causing me problems when I lookup to it...specificially it returns nothing. My formulas use iserror so prevent errors showing up.

    What I need is a script or macro that searches the range (a1:z20) and finds all cells there the leftmost char is a colon and replaces it with 0:

    Any help on this would be greatly appreciated. (I do 30 pages of this a day and need a faster way to go through them)

    "If at first you don't succeed, destroy all evidence you ever tried."
    http://www.ussretribution.com

  2. #2
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Leading zeros

    The way I understand it, you could do something like this:
    VB Code:
    1. If Left(Cells(row, column).value, 1) = ":" then
    2.     Cells(row, column).value = "0" + Cells(row, column)
    Stick that in a loop, and you should be good.

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Leading zeros

    Are you storing the time as a string or as a number?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    62

    Re: Leading zeros

    as a number is what I'm trying to do. Java, you said to stick it in a loop. Can I ask you to show me how to do a loop as I've never done one successfully?

    "If at first you don't succeed, destroy all evidence you ever tried."
    http://www.ussretribution.com

  5. #5
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Leading zeros

    Sure.
    VB Code:
    1. For i = startingNumber To endingNumber
    2.     If Left(Cells(row, column).value, 1) = ":" then
    3.         Cells(row, column).value = "0" + Cells(row, column)
    4.     End if
    5. Next i

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Leading zeros

    OK, that is not going to work if you are storing values
    VB Code:
    1. For i = startingNumber To endingNumber
    2.     If Left(Cells(row, column).value, 1) = ":" then
    3.         Cells(row, column).value = "0" + Cells(row, column)
    4.     End if
    5. Next i
    . The value of the cell doesn't contain a colon, that only in the format. To find cells that only have seconds you need to look for a cell value less than 0.00069444
    (A day [24 Hrs] = 1.0 therefore 1 min = 0.00069444)
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Leading zeros

    Are you sure? Because I tested it before I posted, and it worked fine for me.

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Leading zeros

    It will work if you are storig a Text String, not if you are storing a datetime value.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9
    Member
    Join Date
    Apr 2005
    Location
    Arlington, TX
    Posts
    60

    Re: Leading zeros

    Well, that's depressing...sorry about that.

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    62

    Re: Leading zeros

    Sorry for the delay. been on a wee bit of a tear. I'll give it a whack tomorrow morning at work and see the magic.

    Thank you both for your help! This place is a godsend!

    "If at first you don't succeed, destroy all evidence you ever tried."
    http://www.ussretribution.com

Posting Permissions

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



Click Here to Expand Forum to Full Width