+ Reply to Thread
Results 1 to 27 of 27

Quick problem - string mix of letters & numbers - increment by one

  1. #1
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Unhappy Quick problem - string mix of letters & numbers - increment by one

    Just posting a quick problem I have. It shouldn't be too hard but I'm under the weather and can't work how to tackle it right now.

    Say you have a string which is a reference number. It has alphabetical characters at the front of the string and numerals for the rest. e.g. "XYZ42567" I need a function that I can pass this string to and it will generate the next string. so from the example, the function would return "XYZ42568".

    Also we can't guess what the letters are for, so if the input argument is "XYZ999" then an input box should be shown to the user that the function can't predict the next code so it will need it manually entered this time.


    To make it easier, I currently only need it for strings that are in the format of letters followed by numerals.

    (But ideally the function should accept a complete mix of numerals & letters. The function only increments the last block of numerals from the right).
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Quick problem - string mix of letters & numbers - increment by one

    is it always 3 letters?
    Please Login or Register  to view this content.
    if it is
    assuming a1 is cell in reference
    Last edited by humdingaling; 06-20-2013 at 12:32 AM.
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    No. I only used 3 letters in the example.

    So as you can see there are few little string tricks that will be needed here. e.g. Detect rightmost non-numeric & RIGHT & LEN etc.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quick problem - string mix of letters & numbers - increment by one

    How about just dragging down the fill handle?
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Quick problem - string mix of letters & numbers - increment by one

    array formula
    shift+ctrl+enter
    Please Login or Register  to view this content.
    does not work if starts with numeric

    extracting number from string found here
    http://office.microsoft.com/en-au/ex...001154901.aspx

    the $9 represents the number of characters so if over 9 increase
    Last edited by humdingaling; 06-20-2013 at 02:59 AM.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Quick problem - string mix of letters & numbers - increment by one

    perhaps
    Please Login or Register  to view this content.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    @ shg & humdingaling = thanks for the suggestions but that is not what I am looking for because:
    1. I don't need this function for an excel range and
    2. I don't know how the end user deals with a part reference like "ABCXYZ999" + 1 (Is it "ABCXZA000"? is it "ABCXZA001"? is it "ABCXYZ1000"? I don't know. Not sure the end user is either! ) So the end user needs to be prompted if this happens.

    @ JosephP = once again you provide the answer. +1


    I'll mark this as solved but I'm uneasy about using vbscript through VBA. How compatible is this?

    UPDATE: Below was solved by shg.
    I'll rep anyone who can provide code that works similar to Josephs but using standard VBA methods only (e.g. LEN, RIGHT, MATCH etc).
    Last edited by mc84excel; 06-20-2013 at 07:25 PM.

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Quick problem - string mix of letters & numbers - increment by one

    it should work on windows versions of excel

  9. #9
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    Quote Originally Posted by JosephP View Post
    it should work on windows versions of excel
    It does.

    I was thinking of Mac users.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Quick problem - string mix of letters & numbers - increment by one

    I guess someone oughta ;-)

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quick problem - string mix of letters & numbers - increment by one

    Please Login or Register  to view this content.

  12. #12
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    Thanks shg. +1

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Quick problem - string mix of letters & numbers - increment by one

    I don't know if it's an issue for what you're doing but shg's current version does not retain leading zeroes in the numbers

  14. #14
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    Thanks for pointing that out. I haven't come across any part or reference numbers with leading zeros before any numbers but I would like the function to handle it if it ever does.

    The output string should be the same character length as the input string with the last set of numerics in the string incremented by one. Any leading zeroes in the last set of numerics in the string should be kept (to maintain string length). Where the input strings last set of numerics are all 9 then the function should return an input box to ask the user for a manual entry - see post #7 for the reasons on this.

    I am thinking the detection part of could be solved by changing this line
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    (untested)

    UPDATE: Below solved by shg
    I'll rep anyone who can edit the code to achieve the outcome in the 2nd paragraph of this post. Thanks
    Last edited by mc84excel; 06-24-2013 at 08:26 PM.

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quick problem - string mix of letters & numbers - increment by one

    Please Login or Register  to view this content.

  16. #16
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    That was quick!

    I'll test it out and get back to you. (What happened to the input box? )

  17. #17
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    Quote Originally Posted by shg View Post
    Please Login or Register  to view this content.
    Close. I tested it and it only works with one leading zero. ("ABCXYZ0999" = works, "ABCXYZ00999" = fail)

    Below is my current version of the code:
    Please Login or Register  to view this content.

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quick problem - string mix of letters & numbers - increment by one

    I tested it and it only works with one leading zero. ("ABCXYZ0999" = works, "ABCXYZ00999" = fail)
    Please Login or Register  to view this content.
    Those all look right to me.

  19. #19
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    I was testing the code in post #17 (uncomment the 3rd last line & comment the 4th last line) with these commands in the immediate window:

    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    - they both gave me the result of ABCXYZ1000

  20. #20
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quick problem - string mix of letters & numbers - increment by one

    Dunno what to tell you.

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

  21. #21
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    Sorry shg. Your code was right all along. My apologies for wasting your time.
    +1 for successful solving as stated in post #14.


    (I was using my version of your code - see post #17 - and it wouldn't handle more than one zero. When I tried your version - post #20 - it worked. Then I realised. I had missed changing one variable. Lesson learnt = Never, never, turn off Option Explicit. I don't need to and it's not worth it! )

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

    Re: Quick problem - string mix of letters & numbers - increment by one

    When Regular Expression is used, the code should be short as
    Please Login or Register  to view this content.

  23. #23
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    Thanks jindon. However I am avoiding using RegEx (see post #7).

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

    Re: Quick problem - string mix of letters & numbers - increment by one

    I knew that.

  25. #25
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: Quick problem - string mix of letters & numbers - increment by one

    My assumption, sorry. BTW I repped you for contributing this code in case it helps another forum user who wishes to use a RegEx version.

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

    Re: Quick problem - string mix of letters & numbers - increment by one

    Ah, thanks for that.

  27. #27
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Quick problem - string mix of letters & numbers - increment by one

    This is a little more robust:

    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)

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