+ Reply to Thread
Results 1 to 16 of 16

Search part of a string using vlookup

  1. #1
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Search part of a string using vlookup

    Hi

    Would anyone be able to advise of the vb code to search a column of text for an exact match and return the row number?

    I have a column of data strings such as "13ABCD00012", "13DFER00118" etc. I have a txtbox "txtPO" which contains the search stricg, say "118". With the code, I want to search the column of data strings for a string that contains "118", say. In this example, it would result in finding
    "13DFER0018" and return, say, 65 as the row number in that column where the full string is.

    Hope this makes sense and hope someone could advise.

    Cheers

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Search part of a string using vlookup

    You wouldn't use VLOOKUP, you would use MATCH to get the row.

    For example

    =MATCH("*" & A1 & "*", B1:B100, 0)

    =INDEX(B1:B100,MATCH("*"&A1&"*",B1:B100,0))

    Note, these are worksheet formulas and the first one returns the row not the column.

  3. #3
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Search part of a string using vlookup

    Hi Norie

    Sounds good using MATCH and INDEX. How would I use these in vba? Should I prefix with Application.WorksheetFunction?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Search part of a string using vlookup

    You never mentioned VBA.

    If you are searching for partial matches in VBA you might be better off with using VBA's Find method.

    If it finds a match it returns a reference to the cell with the match, from which you can get the row number and value.

  5. #5
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Search part of a string using vlookup

    Sorry about that Norie I should have made it clear at the outset about wanting to use VBA. I am a novice in VBA and wonder if you could help start me off with the code?

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Search part of a string using vlookup

    Can you give more information?

    What range in which worksheet will you be looking in?

  7. #7
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Search part of a string using vlookup

    In column A is where all Order Numbers are stored. These Order Numbers look like 13MDGH00013, 13KJYT00017, 13BMLP01076 and so on. All Order Numbers are prefixed with 13 followed 4 letters and followed by 5 numerical digits. I want to search all the Order Numbers in the column A for the one which has, say 1076 as the numerical part. In the example Order Numbers above, it would recognise the Order Number 13BMLP01076 as the one matching the search criteria and will tell me the row that this Order Number is in that column.

    Does this make sense?

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Search part of a string using vlookup

    Yep, makes sense so far.

    Now where's the textbox and what do you want to do with the result if there is a match, or even if there isn't?

  9. #9
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Search part of a string using vlookup

    The textbox (called txtPO) is on a userform. If there is no match, I want a msgbox tell me that there is no match. If there is a match, I want the code to allocate the variable CurRow with that row number.

  10. #10
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Search part of a string using vlookup

    Thanks for being patient, Norie!

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Search part of a string using vlookup

    Here's a small example workbook.

    Might give you a start.
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Search part of a string using vlookup

    Norie

    Your code works and give the results I would be looking for. How can your code be tweaked to input just the numbers without the zeros in the search box? So if I want to search for the Order Number 13DFER00118 I would just put 118 in the search box and the results would be it's in row 4 and the full Order Number is 13DFER00118. I suspect it would involve using left or mid string functions, but I just can't get my head around it!

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Search part of a string using vlookup

    Strange, when I typed 118 in and hit the button it found 13DFER00118 in row 4.

    Did you have to add zeroes for it to work?

  14. #14
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Search part of a string using vlookup

    I typed 118 and the msgbox said nothing found. I tried with 00118 which also said nothing found. It only gave the result when I typed the whole Order Number, ie 13DFER00118. What am I doing wrong?

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Search part of a string using vlookup

    It's probably my fault.

    If you look at the code you'll see the only argument I've supplied for Find is the value from the textbox.

    There are various other arguments that can be used.

    Try changing the code to this.

    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    07-23-2012
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    211

    Re: Search part of a string using vlookup

    Norie

    YEAY!!!! It worked. You have saved me days of searching! Thanks very much. Have a great weekend

+ 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