+ Reply to Thread
Results 1 to 19 of 19

Add leading zeros in same column numbers and text

  1. #1
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Add leading zeros in same column numbers and text

    So quick sample of data :
    B74
    B74
    9
    94
    1
    948
    B74

    So if I have this data in a column you notice they have different lenght.
    Now I want my macro to add leading zeros until the length of all occurrences is 4.

    I know for numbers you can do a range.numberformat = "0000"
    But this won't work for B74 because (obviously) it will not be recognized as a number.

    I know in a formula you can do it with the command TEXT.
    However I do not want to create extra columns I want something to alter it in place just like the numberformat does
    but then for numbers and text.
    Thanks!

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,063

    Re: Add leading zeros in same column numbers and text

    There may be more elegant solutions, but this does work (assumes data start in a1)
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  3. #3
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Add leading zeros in same column numbers and text

    Highlight the TOP of the column and run this macro - this assumes there are no BLANKS in the column

    FORMAT THE COLUMN FOR TEXT PRIOR TO RUNNING


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by GeneralDisarray; 07-29-2014 at 09:36 AM.
    Remember, saying thanks only takes a second or two. Click the star icon(*) below the post you liked, to give some Rep if you think an answer deserves it.

    Please,mark your thread [SOLVED] if you received your answer.

  4. #4
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Re: Add leading zeros in same column numbers and text

    Thanks Glenn but not looking fot a formula. As stated that would be even simpler using TEXT formula.

  5. #5
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Re: Add leading zeros in same column numbers and text

    Thanks generaldisarray, so there is no simple function in VBA like numberformat which would do this?
    And how is the performance of this solution with more the 100000 lines, do you know?
    Anyway thanks for your solution

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Add leading zeros in same column numbers and text

    Here's how I read your problem
    Attached Files Attached Files

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Add leading zeros in same column numbers and text

    Another way:

    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Re: Add leading zeros in same column numbers and text

    Thanks Jindon,
    But actually B74 is ok and would become 0B74
    However an interesting piece of code, thank you!

  9. #9
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Re: Add leading zeros in same column numbers and text

    Thanks SHG, your code would probably work too.
    So my conclusion would be that there is no real "easy" way of transforming all in the column to length 4
    Like this:
    Please Login or Register  to view this content.
    Too bad.

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Add leading zeros in same column numbers and text

    Then this should do.
    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Re: Add leading zeros in same column numbers and text

    Thanks jindon.

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Add leading zeros in same column numbers and text

    You are welcome and thanks for the rep.

  13. #13
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Add leading zeros in same column numbers and text

    formula =REPT("0",4-LEN(A1))&A1
    or maybe
    code
    Please Login or Register  to view this content.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  14. #14
    Forum Contributor
    Join Date
    07-26-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2016 Office 365 ProPlus
    Posts
    822

    Re: Add leading zeros in same column numbers and text

    Thanks Martin,
    This is not as short as I would want but it is the shortest yet

  15. #15
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Add leading zeros in same column numbers and text

    Yeah, i would really just recommend using another column if it's at all possible Looping over that many rows is never going to be speedy.

    There might be a way to do the entire range in one shot so there is no loop, but i can't think of it. Usually SHG comes up with magic like that...

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Add leading zeros in same column numbers and text

    Usually SHG comes up with magic like that...
    Thanks, I think, but I'm no fan of brevity for its own sake.

    As one wag once said, if most architects designed buildings like most programmers design code, the first woodpecker to come along would destroy civilization.
    Last edited by shg; 07-29-2014 at 11:16 AM.

  17. #17
    Forum Expert GeneralDisarray's Avatar
    Join Date
    09-15-2011
    Location
    Pittsburgh, PA, USA
    MS-Off Ver
    Windows Excel 2016
    Posts
    1,416

    Re: Add leading zeros in same column numbers and text

    I meant it as a complement for sure.

    I agree, when I try to hard to make code short I end up confusing myself. Some workbook I assembled in 2011 was recently brought back to my attention - a customer wanted a "proof" of the underlying methodology.... Spent a morning figuring out what I had done :S

  18. #18
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,063

    Re: Add leading zeros in same column numbers and text

    Sorry. i didn't notice the forum that you'd posted in...

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Add leading zeros in same column numbers and text

    Quote Originally Posted by GeneralDisarray View Post
    Yeah, i would really just recommend using another column if it's at all possible Looping over that many rows is never going to be speedy.

    There might be a way to do the entire range in one shot so there is no loop, but i can't think of it. Usually SHG comes up with magic like that...
    My code in #12 is not looping.

+ 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. Concatinating numbers with leading zeros
    By pretom1 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 06-28-2011, 03:05 AM
  2. Leading Zeros-display the numbers from 1 to 9
    By Sathiyabama in forum Excel General
    Replies: 3
    Last Post: 04-13-2011, 02:19 AM
  3. [SOLVED] Combining Numbers that Have Leading Zeros
    By Ravee Mathi Sundar in forum Tips and Tutorials
    Replies: 1
    Last Post: 08-23-2005, 03:34 AM
  4. [SOLVED] sort numbers leading zeros
    By l smith in forum Excel General
    Replies: 2
    Last Post: 06-08-2005, 11:05 AM
  5. Replies: 1
    Last Post: 05-04-2005, 02:06 PM

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