+ Reply to Thread
Results 1 to 5 of 5

Determine Length in a Macro

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Determine Length in a Macro

    I want to say something like this in a Macro, but I'm not sure how to write it:

    =IF(LEN(A1)=6,"00"&A1,IF(LEN(A1)=7,"0"&A1,A1))

    Ideally, what I want to say is, that if the selected range (rather than a specific cell) contains less than 8 numbers (or even better a user defined number of characters, via a text entry dialogue), then put leading zero's before the number. I would then like to convert the selected range to Text format, which I could do using a seperate macro, but it would be nice if it can be achieved all in the same macro.

    Is this possible to do?

    Many thanks

  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,440

    Re: Determine Length in a Macro

    =IF(LEN(A1)=6,"00"&A1,IF(LEN(A1)=7,"0"&A1,A1))

    Please Login or Register  to view this content.

    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
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Determine Length in a Macro

    Or:

    Please Login or Register  to view this content.

  4. #4
    Forum Expert contaminated's Avatar
    Join Date
    05-07-2009
    Location
    Baku, Azerbaijan
    MS-Off Ver
    Excel 2013
    Posts
    1,430

    Re: Determine Length in a Macro

    Please Login or Register  to view this content.
    Люди, питающие благие намерения, как раз и становятся чудовищами.

    Regards, ?Born in USSR?
    Vusal M Dadashev

    Baku, Azerbaijan

  5. #5
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Determine Length in a Macro

    Am I missing something? I tried all three variations and neither (for me) are changing my range of numbers to eight characters?

    What I would like to be able to do, is to make this into a user definable number of leading zero's and at the same time change the format of the selected cells to 'Text'... Is this possible?

    Many thanks

+ 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