+ Reply to Thread
Results 1 to 8 of 8

VBA to add "0" at the beginning depending on cell value length

  1. #1
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    VBA to add "0" at the beginning depending on cell value length

    Hi there

    I was thinking about creating a code that would add automatically a "0" at the beginning of the cell value if its length is 12. I realise I could play around with some formulas and get the result but I'd like to know how to approach this in VBA.

    Eg. in reports I get numbers that in some cases I want as full numbers and not cut short like getting "99123000123" instead of "099123000123".

    I've come up with the below which doesn't quite work... I also checked how VBA expects the number formats and the one that could help is "0"#, however in VBA I'd need to put it as ""0"#" which makes it impossible.

    No idea though why it doesn't work in the first place but I'm quite bad at these things so that's probably the reason


    Please Login or Register  to view this content.
    Cheers

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: VBA to add "0" at the beginning depending on cell value length

    Format the cells in column A as a number with no decimal places and try this macro:
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

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

    Re: VBA to add "0" at the beginning depending on cell value length

    Try one of these
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: VBA to add "0" at the beginning depending on cell value length

    Hiya,

    Amazing stuff, works like charm, both of these actually.

    I was wondering (not being too much of an expert, learning these things as I go), if I put these codes into the ThisWorkbook code - will it automatically change the A2:A500 cells without the need to run the macro?

    If it's possible, do I put it as a Public Sub/Private Sub? Does it change anything?

    Sorry if I'm being dense
    Cheers

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

    Re: VBA to add "0" at the beginning depending on cell value length

    Why not just format the cells in the column as "0000000000000"?

  6. #6
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: VBA to add "0" at the beginning depending on cell value length

    My apologies. I have a small error in the code I posted. Please used the revised macro if it is helpful.
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-28-2015
    Location
    Krakow
    MS-Off Ver
    2003/2013
    Posts
    37

    Re: VBA to add "0" at the beginning depending on cell value length

    Quote Originally Posted by jindon View Post
    Why not just format the cells in the column as "0000000000000"?
    Because in that column I also have other numbers that are shorter and it gives them a nasty look and the zeros would need to be deleted for other uses.

    Eg. 991230000123 would become 0991230000123 correctly, but 1223334444 would become 0001223334444 and if I wanted to copy and paste it in other places it would become a nuisance

    @Mumps1 yeah I noticed this one it works nicely.

  8. #8
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,821

    Re: VBA to add "0" at the beginning depending on cell value length

    Glad it helps.

+ 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. macro to record value "1" depending on another cell value being highest in range
    By semajjames in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-18-2015, 07:24 PM
  2. Digital Signatures - Move "pictures" depending on certain cell value
    By J0ck3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-12-2013, 08:25 AM
  3. Move text (the word "The") to beginning of cell
    By techstorm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-27-2012, 03:04 PM
  4. typing "/" at beginning of cell brings up short cut characters - how shut off?
    By roccinstaller in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 12-13-2011, 04:51 PM
  5. Auto populate "text" in a cell depending on the "text" in another cell
    By shenoyhr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-05-2011, 01:33 AM
  6. Open a workshhet depending on a cell "name"
    By tromao in forum Excel General
    Replies: 2
    Last Post: 07-14-2007, 12:27 PM
  7. Identification of "<" character at beginning of cell
    By Hemil in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-26-2005, 05:15 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