+ Reply to Thread
Results 1 to 15 of 15

Add number sequentially to string based on formula

  1. #1
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Add number sequentially to string based on formula

    I have the following macro that runs beautifully that replicates a sheet in my workbook and removes rows from the original sheet based on certain criteria.
    I wish to amend this code so that the resulting string that remains in cells A17:A190 is effected by the following formula:

    If A18 is blank then leave as blank

    =IF(MID(A18,8,1)="/" then left(a18,8)+mid(a18,9,3) +1
    so if there is P009959/004 in A18 the macro places P009959/005 in its place.

    =IF(LEFT(A18,1)="S" then left(a18,10)+mid(a18,10,3)+1
    so if there is SP0091377/001A in A18 the macro places SP0091377/002 in its place

    otherwise
    LEFT(A18,10)


    here is my macro so far

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by nickmax1; 09-07-2012 at 09:10 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Add number sequentially to string based on formula

    is this possible?

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

    Re: Add number sequentially to string based on formula

    Is this kind of what you are after?
    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Add number sequentially to string based on formula

    jindon that works great for the 1st formula ( ie it takes care of the string with a "/" in it) but can the code be amended to deal with excel formula 2 ( dealing with string beginning with S)>>?

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

    Re: Add number sequentially to string based on formula

    I don't understand...

    It should convert as you wanted regardless of "S"...

    Can you explain further?

  6. #6
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Add number sequentially to string based on formula

    If I read your code right it only deals with string with a / in it. I if you see my formulas above I also want to deal with strings starting with a S.
    Try the formula on my Aug 2012 sheet and you can see not all strings are dealt with

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

    Re: Add number sequentially to string based on formula

    Your formulas doesn't make sense to me.

  8. #8
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Add number sequentially to string based on formula

    ok i will try to explain.
    Your code works brilliantly for my formula 1, adding a number to the string that has a "/" in there somewhere.

    I also want it to add a number to the string when it starts with the letter S (for example A42 has one). For the strings without a / in it or that begin with an S, such as P01132F11A, then this is to be converted to P01132F12A

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

    Re: Add number sequentially to string based on formula

    OK, can you upload the file with before / after format for possible patterns?

  10. #10
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Add number sequentially to string based on formula

    BeforeAfter.xlsx

    heres a few examples with what they are before and what i am hoping the macro will change them to.

    Also if the cell is blank the macro needs to do nothing.

    thanks!

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

    Re: Add number sequentially to string based on formula

    Quote Originally Posted by nickmax1
    I have removed any " - R" entries so that isnt an issue going forward.
    Then,
    Please Login or Register  to view this content.
    Last edited by jindon; 09-09-2012 at 08:32 AM.

  12. #12
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Add number sequentially to string based on formula

    I have removed any " - R" entries so that isnt an issue going forward.

  13. #13
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Add number sequentially to string based on formula

    works brilliantly!!

    i have NO idea how your code does what it does, but it works! Can you run through what each line of code does?

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

    Re: Add number sequentially to string based on formula

    It adds 1 to the last appeared block of numbers.

    "(.*\D)(\d+)(\D+)?$"

    \d = Number
    \D = Non numeric character

    This pattern will match to

    Any characters + numbers with or without any character(s) that is/are not number at the end.
    And you can refer back to each sub-matches that is inside the set of brackets afterwords.

    For more details, google with the key words like "VBA" "Regular Expression" "Meta Character" etc.

  15. #15
    Valued Forum Contributor
    Join Date
    08-10-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    721

    Re: Add number sequentially to string based on formula

    thank you so much for your prompt help and explanation!

+ 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