+ Reply to Thread
Results 1 to 4 of 4

Adding Leading Zero's

  1. #1
    Registered User
    Join Date
    06-28-2012
    Location
    Shawano, WI
    MS-Off Ver
    MS365 version 2310
    Posts
    99

    Adding Leading Zero's

    Hello,
    I would like to add leading zero’s to VBA and I’m not having much luck. What I am trying to do is break up a line of text and then make sure to add leading zero’s to an official ID. When I write a normal formula it would be something like =text(a1,”000000000000”) but when I add this to the VBA it just puts that part of the VBA in the cell. I need the numeric part of the ID to be 12 digits.
    For example, HOCAN123456789012.

    I have attached a spreadsheet for reference and the code below as well.

    Thank you in advance!


    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Adding Leading Zero's

    =right(string(12,"0") & range("a1").value,12)

    that should do it, not in excel.

    a1 is where the data to add leading 0's to is.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Registered User
    Join Date
    06-28-2012
    Location
    Shawano, WI
    MS-Off Ver
    MS365 version 2310
    Posts
    99

    Re: Adding Leading Zero's

    I'm having trouble with what you are stating. I might not have explained this real well. I would like to start with location number 159 and take the next 5 spaces and then start with location 165 and then the next 12 spaces. Since most of the time I need to add the leading zero's to the numbers that are populating the text document starting at space 165. Most of the time it is 6-8 numbers.

    Right now when I add the code provided it gives me an error. How I'm reading the code provided I would just be adding leading zero's to what ever is listed in A1. Though I might not be reading this correctly.

    Below is what is not working.



    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    06-28-2012
    Location
    Shawano, WI
    MS-Off Ver
    MS365 version 2310
    Posts
    99

    Re: Adding Leading Zero's

    I'm having trouble with what you are stating. I might not have explained this real well. I would like to start with location number 159 and take the next 5 spaces and then start with location 165 and then the next 12 spaces. Since most of the time I need to add the leading zero's to the numbers that are populating the text document starting at space 165. Most of the time it is 6-8 numbers.

    Right now when I add the code provided it gives me an error. How I'm reading the code provided I would just be adding leading zero's to what ever is listed in A1. Though I might not be reading this correctly.

    Below is what is not working.



    Please Login or Register  to view this content.

+ 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. Help adding leading and trailing zeros!
    By vpsaline in forum Excel General
    Replies: 4
    Last Post: 10-16-2013, 10:48 PM
  2. Adding leading zeros to a text box
    By fbiasi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2013, 01:53 PM
  3. Excel 2007 : Adding leading zeros to 35K lines
    By budchevy in forum Excel General
    Replies: 2
    Last Post: 05-08-2011, 08:50 PM
  4. [SOLVED] adding a leading 0 to only the numbers with 4 digits
    By SwampYankee in forum Excel General
    Replies: 4
    Last Post: 06-01-2006, 03:55 PM
  5. [SOLVED] Adding leading spaces
    By gcotterl@co.riverside.ca.us in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-24-2006, 02:25 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