+ Reply to Thread
Results 1 to 7 of 7

Preserve leading zeros in a varying length number when converting it to text

  1. #1
    Registered User
    Join Date
    11-02-2012
    Location
    Green Bay
    MS-Off Ver
    Excel 2007
    Posts
    3

    Preserve leading zeros in a varying length number when converting it to text

    Hi, I've been struggling with this for a while now, hopefully someone will be able to help.

    I have a column of numbers of varying lengths that may or may not have leading zeros. Using VBA I need to concatenate the column to another text column.

    the number column might contain
    00123
    0120
    0000043200
    2453230
    001234567890

    Sometimes the cell contents are formatted as general, sometimes numbers, sometimes they have the special formatting set to a certain length of number e.g. 0000000.
    Whichever way the cell is formatted I need to convert it to text while keeping the leading zeros.
    I'm converting a customers worksheet and can't control how they're entering their data.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,451

    Re: Preserve leading zeros in a varying length number when converting it to text

    Maybe (untested):

    =TEXT(N(A2),"000000000000")

    And copy down. Modify column as required.


    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    11-02-2012
    Location
    Green Bay
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Preserve leading zeros in a varying length number when converting it to text

    I need to preserve the length of the number, can't have them all being 12 characters long.

    Thanks though

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

    Re: Preserve leading zeros in a varying length number when converting it to text

    =TEXT(A1,REPT("0",LEN(A1)))

    VBA?
    Example
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-02-2012
    Location
    Green Bay
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Preserve leading zeros in a varying length number when converting it to text

    Thanks jindon, that works great!

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,451

    Re: Preserve leading zeros in a varying length number when converting it to text

    Maybe:

    =TEXT(N(A1),REPT("0",LEN(A1)))


    Regards, TMS

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

    Re: Preserve leading zeros in a varying length number when converting it to text

    Actually
    Please Login or Register  to view this content.
    Should be enogh...

    Note: If the cell is not wide enough to display all the contents and appears like "######", .Text property will get such value as it get as you see in the cell.
    Last edited by jindon; 02-01-2013 at 12:52 AM.

+ 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.6.0 RC 1