+ Reply to Thread
Results 1 to 7 of 7

VBA to add leading zero.

  1. #1
    Forum Contributor
    Join Date
    11-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2016
    Posts
    140

    VBA to add leading zero.

    I am trying to run VBA code to add a leading zero so that the figures are 2 in length;
    e.g. 1 becomes 01

    I have looked around and found some code, it doesn't error but it doesn't add a leading zero when run.
    Please Login or Register  to view this content.
    I have attached an example sheet.
    Attached Files Attached Files

  2. #2
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,014

    Re: VBA to add leading zero.

    https://excelchamps.com/blog/leading-zeros/

    Look at #5

  3. #3
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to add leading zero.

    Or try this:

    Please Login or Register  to view this content.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA to add leading zero.

    Here is another macro (no loops) that should also work...
    Please Login or Register  to view this content.

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: VBA to add leading zero.

    Actually, in thinking about it some more... given the OP's layout, we only need to examine up to Row 57 (that is, the cells containing either blanks or the first 9 numbers) since after that, all number cells will consist of two digits (assuming the OP's numbering stops at 99 maximum). Given that, this macro should also work...
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    11-08-2012
    Location
    Manchester, UK
    MS-Off Ver
    Excel 2016
    Posts
    140

    Re: VBA to add leading zero.

    Thank you all for your help, I have gone with Rick's last submission with a little edit :-D
    Please Login or Register  to view this content.

  7. #7
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: VBA to add leading zero.

    You're welcome and thanks for the rep!

+ 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 with leading 0's
    By pleasir in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-07-2017, 07:18 PM
  2. Leading Zero's
    By HangMan in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-10-2013, 10:21 PM
  3. leading Zero
    By mma3824 in forum Excel General
    Replies: 2
    Last Post: 10-31-2012, 11:45 AM
  4. Leading zero
    By rofl in forum Excel General
    Replies: 4
    Last Post: 01-24-2010, 04:07 PM
  5. Leading 0's
    By exsam21 in forum Excel General
    Replies: 5
    Last Post: 01-20-2006, 02:40 PM
  6. [SOLVED] leading 0
    By cb in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-13-2005, 10:06 AM
  7. [SOLVED] Add a leading zero
    By yahoo in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 03-12-2005, 03:06 PM

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