+ Reply to Thread
Results 1 to 12 of 12

Thread: Return Location of Alpha/Numeric String

  1. #1
    Registered User
    Join Date
    08-21-2010
    Location
    Omaha, NE
    MS-Off Ver
    Excel 2003
    Posts
    40

    Return Location of Alpha/Numeric String

    Hello,

    How can I formulaically find the starting location of a string that is letter + 6 numbers + letter in a longer string?

    As in,

    Find 'a123456b' from po34ksa123456b95043, with an answer of 7.

    I was thinking FIND() with wild cards, but the wold cards would have to specify whether it was text or numeral. Any help appreciated!

    Greg
    Last edited by Greg777; 10-27-2011 at 08:18 PM.

  2. #2
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Return Location of Alpha/Numeric String

    Why would you need wildcards?

    =FIND("a123456b","po34ksa123456b95043",1)
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Return Location of Alpha/Numeric String

    if you KNOW what the string is ahead of time, a simple SEARCH() or FIND() function will return that position for you.

    If you are asking Excel to find ANY string that is in that syntax, then you would need a new function written for you. It would be a VBA solution.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    08-21-2010
    Location
    Omaha, NE
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Return Location of Alpha/Numeric String

    Sorry, to clarify, meant that as an example. I don't know the composition of the string, just it's format. I've attached an excel sheet as a further example of what I am wanting to do.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    08-21-2010
    Location
    Omaha, NE
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Return Location of Alpha/Numeric String

    I'll mark this one solved and re-post in the programming section.

  6. #6
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Return Location of Alpha/Numeric String

    Or you could just ask a mod to move it for you.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  7. #7
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Return Location of Alpha/Numeric String

    Moved to Programming
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  8. #8
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Return Location of Alpha/Numeric String

    For a programming solution, this new function will do what you ask:

    Function EightCode(MyStr As String) As String
    Dim m
    
    With CreateObject("VBScript.RegExp")
        .Pattern = "[A-Za-z]\d{6}[A-Za-z]"     'letter-6 digits-letter
        For Each m In .Execute(MyStr)
            EightCode = m.Value
        Next m
    End With
    
    End Function

    How to install the User Defined Function:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save as a macro-enabled workbook

    The function is installed and ready to use.


    Now, use this function in a cell like so:

    =EIGHTCODE(A2)


    To get the "position" over in your table, then use this in K2:

    =SEARCH(EIGHTCODE(J2),J2)
    Attached Files Attached Files
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  9. #9
    Registered User
    Join Date
    08-12-2011
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    49

    Re: Return Location of Alpha/Numeric String

    This works properly:
    Function FindCustom(strng As String)
    FindCustom = "NOT IN STRING"
    For i = 1 To Len(strng)
        test = Mid(strng, i, 8) Like "[a-zA-Z]######[a-zA-Z]"
        If test = True Then
            FindCustom = i
            Exit Function
        End If
    Next i
    End Function
    Last edited by yay_excel; 10-27-2011 at 05:34 PM.

  10. #10
    Registered User
    Join Date
    08-21-2010
    Location
    Omaha, NE
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Return Location of Alpha/Numeric String

    thanks! they both work, the first giving the string and the most recent post gives the location. i can see how they function and should be able to modify them for other purposes later. help much appreciated

  11. #11
    Forum Guru JBeaucaire's Avatar
    Join Date
    03-21-2008
    Location
    Bakersfield, CA
    MS-Off Ver
    2010
    Posts
    19,226

    Re: Return Location of Alpha/Numeric String

    I'm not sure if you read my entire post. My Function is designed to find the string, true, but then I showed you how to get the location using standard SEARCH function. So mine allows you to use it for both purposes, to identify the string itself, then use it directly in other functions.

    =SEARCH(EIGHTCODE(J2),J2)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    “None of us is as good as all of us” - Ray Kroc
    “Actually, I *am* a rocket scientist.” - JB (little ones count!)

  12. #12
    Registered User
    Join Date
    08-21-2010
    Location
    Omaha, NE
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Return Location of Alpha/Numeric String

    Understood. Works great. Thanks!

+ 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.2.0