+ Reply to Thread
Results 1 to 4 of 4

Leading zeros needed

  1. #1
    Registered User
    Join Date
    05-14-2010
    Location
    Arizona
    MS-Off Ver
    Excel 2007
    Posts
    27

    Leading zeros needed

    I have two columns (A & B) in a CSV file. They have numbers ranging from 1 to 3000 with repeats. I need all values to be at LEAST 4 characters long (i.e. 4 = 0004), but anything larger than 4 is okay (just 4 or less needs to be adjusted).

    What I can't do: Format the columns and make them Custom 0000
    I need these values to be inserted back into SQL. Custom 0000 still leaves the value as 4 (using the example above).

    I thought doing custom 0000 and then running my macro would work, but it didn't.

    Macro:

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Derek

  2. #2
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Leading zeros needed

    Here's a formula you can use

    =RIGHT("0000"&A1,MAX(4,LEN(A1)))

    You should be able to convert that into VBA code as well if you like.

  3. #3
    Valued Forum Contributor Lemice's Avatar
    Join Date
    04-13-2013
    Location
    Somewhere.
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Leading zeros needed

    Hello,

    You can "force" them into text with =TEXT(A1,"0000") for example, if the number is in cell A1.
    Book1.csv
    (copy pasta from Ford)
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools

    Regards,
    Lem

  4. #4
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,578

    Re: Leading zeros needed

    or working with your macro, try this
    Please Login or Register  to view this content.
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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