+ Reply to Thread
Results 1 to 15 of 15

VBA code for extracting last three digits from alphanumeric string.

  1. #1
    Registered User
    Join Date
    03-27-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    7

    Cool VBA code for extracting last three digits from alphanumeric string.

    Can you assist me with a code to extract the last three digits from this alpha string AKAKAK44000XXXXXXX164. I do not want the =Right function.....I want a VBA Code.

    then after I extract the last three digits I would like the code to also include a replacement macro that will read something like:

    Cells.Replace What:="164", Replacement:="ABA", LookAt:=xlPart, SearchOrder:=xlByRows

    It's pretty simple and I can do it using a worksheet function but I want something more efficient.

    Thank You.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA code for extracting last three digits from alphanumeric string.

    This isn't how I would normally do it but it since you asked for assistance I figured I would incorporate what you already came up with

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,535

    Re: VBA code for extracting last three digits from alphanumeric string.

    Try this on a copy of your workbook

    Please Login or Register  to view this content.
    You might need to change "UsedRange" with something like

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    03-27-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA code for extracting last three digits from alphanumeric string.

    Thanks for including my work. I'm new to this and still learning. I like your code because I understand it clearly. It works perfectly. But, how do I make the replaced values show up in another column.

  5. #5
    Registered User
    Join Date
    03-27-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA code for extracting last three digits from alphanumeric string.

    Its not working. I get a 400. I changed the used range.

    The alphanumerics are in column A I would like the replace results to show in column B.

  6. #6
    Valued Forum Contributor xlbiznes's Avatar
    Join Date
    02-22-2013
    Location
    Bahrain
    MS-Off Ver
    Excel 2007
    Posts
    1,223

    Re: VBA code for extracting last three digits from alphanumeric string.

    Hi,

    attached is my solution , hope this helps.
    Attached Files Attached Files
    Happy Computing ,

    Xlbiznes.

    To show your appreciation please click *

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,535

    Re: VBA code for extracting last three digits from alphanumeric string.

    Try on a copy of your workbook.
    Columns B and C should be empty.

    Please Login or Register  to view this content.
    Last edited by jolivanes; 03-28-2013 at 04:56 PM.

  8. #8
    Registered User
    Join Date
    03-27-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA code for extracting last three digits from alphanumeric string.

    Yes, it's great the only thing is I want the Column C to contain only the replacement text and none of the alphanumeric code....just the replacement text. Thanks for you time!

  9. #9
    Registered User
    Join Date
    03-27-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA code for extracting last three digits from alphanumeric string.

    Syntax error @ c.Offset(,1),Value = strReplaced

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,535

    Re: VBA code for extracting last three digits from alphanumeric string.

    If you have a very large range, this might be faster.

    Please Login or Register  to view this content.
    Column C should only have the replaced last 3 digits.
    This is not want you want obviously.
    Isn't the replacement text "ABA"?

    Re: Syntax error.
    Change the comma (,) to a decimal (.)
    Last edited by jolivanes; 03-28-2013 at 04:56 PM. Reason: Answer syntax error

  11. #11
    Registered User
    Join Date
    03-27-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA code for extracting last three digits from alphanumeric string.

    I really like this code! Impressive.... It almost there. What I have is 300 lines of alphanumerics. Each has different last three digits. For example CTKOTX540008520XXX532, SUNFX540000XXX425.

    The last three digits of each alphanumeric code has meaning. For instance 532 means Compliance and 425 means National. There is a total of 30 variations of the last three digits, each variation has meaning.

    I would like a code to create three columns within the worksheet to look like this.

    Column A Column B Column C
    CTKOTX540008520XXX532 532 Compliance
    SUNFX540000XXX425 425 National

    The goal here really is COLUMN C with only the text that matches the three digit code.

    I will need the ability to input the values into the code that would generate in Column C. I think the code you have provided is what I'm looking for. Could you tweek it to adjust.
    Last edited by klantacook; 03-28-2013 at 05:27 PM.

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,535

    Re: VBA code for extracting last three digits from alphanumeric string.

    Are these 30 variations with their numbers on a different sheet and if so where on that sheet and what is that sheet's name?
    Don't you want the "ABA" to replace the three digits anymore?

  13. #13
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,535

    Re: VBA code for extracting last three digits from alphanumeric string.

    You could try this if the variations with their respective 3 digit numbers are not in your workbook.
    You'll have to change and expand both Arrays (arr1 and arr2) to reflect these variations and 3 digit numbers.
    The variations have to be indexed (in the same place) as the corresponding 3 digit numbers.
    If you look at the code, that means that 532 corresponds with Compliance, 425 with National etc etc.
    When you have changed the arrays you should have the same amount of entries in arr2 as you have in arr1.



    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    03-27-2013
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    7

    Re: VBA code for extracting last three digits from alphanumeric string.

    Got it!

    I updated the array to match the desired replacements. I couldn't correct the string to pull the last three digits into another column but I could get it to leave it in the remaining column and it works! I didn't understand the Application match potion of the language but left it iand it works nicely. Thank You! and All the BEST!

  15. #15
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,535

    Re: VBA code for extracting last three digits from alphanumeric string.

    I don't know how large the range you're working with is but if you use formulae, it should be considerably faster.
    On my old computer, using formulae is about 5 times faster witha 16,000 cell range.
    If you'd like to try that you should attach a stripped down version of your workbook.

+ 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