+ Reply to Thread
Results 1 to 20 of 20

Partial match using LEFT() in combination with MATCH

  1. #1
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Partial match using LEFT() in combination with MATCH

    Hi All,

    I've been messing around with a formula in Excel 2003, and am now getting to a point where I'm thinking about jumping out of the window. I hope you can help me out.

    My problem is as follows:

    I have a working formula: =INDEX($A$2:$B$1000,MATCH(D2,$A$2:$A$1000,FALSE),2)
    My Sheet looks like this:
    Please Login or Register  to view this content.
    Result = Eggs

    However, I want to be a bit more flexible as the content in Col D can vary slightly.
    For example:
    Please Login or Register  to view this content.
    Result: N/A

    Therefore, I want to use the LEFT function.
    When I use the following code: =LEFT(D2,5)
    Result: 11011

    So far so good. So I thought I could inject it into my existing formula... the result however doesn't work out.

    Formula:
    =INDEX($A$2:$B$1000,MATCH(LEFT(D2,5),$A$2:$A$1000,FALSE),2)

    Result: N/A

    When I check the calculation steps, it gives me the following feedback:
    INDEX($A$2:$B$1000,MATCH("11011",$A$2:$A$1000, FALSE),2),

    To me it looks like all the ingredients are in place. I've tried playing around with wildacrads and brackets, but can;t seem to make it work.


    Do you have any suggestions?

  2. #2
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Partial match using LEFT() in combination with MATCH

    Perhaps?

    =INDEX($b$2:$B$1000,MATCH(LEFT(D2,5),$A$2:$A$1000,FALSE),2)

    Or even better.

    =INDEX($b$2:$B$1000,MATCH(LEFT(D2,5),$A$2:$A$1000,0))
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

  3. #3
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Partial match using LEFT() in combination with MATCH

    The problem is that LEFT (and RIGHT and MID among others) functions return TEXT strings.
    so MATCH is now looking for a Text string in a range of numbers in column A = No match.

    Try LEFT(...)+0 to convert the result of LEFT to a real number.

  4. #4
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    Quote Originally Posted by Jonmo1 View Post
    The problem is that LEFT (and RIGHT and MID among others) functions return TEXT strings.
    so MATCH is now looking for a Text string in a range of numbers in column A = No match.

    Try LEFT(...)+0 to convert the result of LEFT to a real number.
    Thank you, you gave me the right hint. Putting + 0 behind the LEFT() worked like a charm. An other option is to place a -- in front of the LEFT.

    IE: =INDEX($A$2:$B$1000,MATCH(LEFT(D2,5)+0,$A$2:$A$1000,FALSE),2)

    OR =INDEX($A$2:$B$1000,MATCH(--LEFT(D2,5),$A$2:$A$1000,FALSE),2)

    I feel so stupid not noticing my error, as I read about the -- option while going through some vlookup examples.

    @Fotis, thanks for your input, however your suggestions gave the same result (N/A) probably due to the point Jonmo1 explained.

  5. #5
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Partial match using LEFT() in combination with MATCH

    Great, glad to help.

  6. #6
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    Darn, I spoke too soon

    Some of the codes I'll be matching consist of both numbers and alphabetical characters. Am I correct when I assume that I can't combine them in this formula, because you can match either text or numbers, but no combination of the two?

  7. #7
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Partial match using LEFT() in combination with MATCH

    Yep, mixed data is going to be problematic.

    You'll have to test if the value your looking up is numeric or text..

    Try
    =INDEX($A$2:$B$1000,MATCH(IF(ISNUMBER(LEFT(D2,5)+0),LEFT(D2,5)+0,D2),$A$2:$A$1000,FALSE),2)

  8. #8
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    That should work if I had only text or numbers in a cell, but I have both
    Some of the codes I'll be matching look like this:

    1320G-2
    1131X-3

    I've allso noticed that while cleaning up the formula and pulling in the data from different sheets, I can't include the ROW funtion tag any more.

    This works
    =INDEX('Spec list'!$A$2:$B$1000,MATCH(LEFT('MASTER LINELIST'!J13,5)+0,'Spec list'!$A$2:$A$1000,FALSE),2)

    But this doesn't
    =INDEX('Spec list'!$A$2:$B$1000,MATCH(LEFT('MASTER LINELIST'!J:J,ROW(),5)+0,'Spec list'!$A$2:$A$1000,FALSE),2)

    I get the message, to many arguments. I'm begining to wonder if it might me a good idea to create a calculation sheet to grab all the data so I can use smaler formula's nsteed of cramming it all into one formula.
    Last edited by bluerabbit; 04-15-2013 at 09:52 AM.

  9. #9
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Partial match using LEFT() in combination with MATCH

    But column A would be
    1320G
    1131X

    =INDEX($A$2:$B$1000,MATCH(IF(ISNUMBER(LEFT(D2,5)+0),LEFT(D2,5)+0,LEFT(D2,5)),$A$2:$A$1000,FALSE),2)

  10. #10
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    Quote Originally Posted by Jonmo1 View Post
    But column A would be
    1320G
    1131X

    =INDEX($A$2:$B$1000,MATCH(IF(ISNUMBER(LEFT(D2,5)+0),LEFT(D2,5)+0,LEFT(D2,5)),$A$2:$A$1000,FALSE),2)
    I'll try an wrap my head around this one. As a real greenhorn that just started playing with Excel yesterday I need some time to figure out what a function actually does and how you use it.

    If I understand correctly, you say the following, if the first 5 characters are a number, then use LEFT(D2,5)+0, else use LEFT(D2,5). As a value of 1302G can not be converted into a number, you can then look for a text string?

    I've tested your code, and it works in most of the cases. I now run into an N/A output when I use match search for a value with 6 figures or more.

    IE:

    Works:
    1302G-2
    1302G-3
    61011-222
    31011-2235x

    Doesn't work:
    251011

    As soon as I use 6 or more consecutive numbers, the formula doesn't work. This surprises me as the formula looks at the first 5 characters right?
    Last edited by bluerabbit; 04-15-2013 at 10:18 AM.

  11. #11
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Partial match using LEFT() in combination with MATCH

    Yep that's pretty much it.
    If LEFT(...,5)+0 is a number, then use it, otherwise use just the Left(...,5) without the +0

    Provided column A is also a mixture of Numbers and Alpha Characters.
    here's a combination of the colA values you've provided in all posts..

    11011
    1320G
    1131X

  12. #12
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    Quote Originally Posted by Jonmo1 View Post
    Here's a combination of the colA values you've provided in all posts..

    11011
    1320G
    1131X
    That is correct, there are a lot more codes, aprox 200 of them. Most of the Col A values are in the format of 5 characters, but some of them have six characters.

    While typing this, I suddenly realise my flaw. The can be no match if I cut back a 6 character input to 5 characters and after that go and match it with the 6 character counterpart silly me!

  13. #13
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Partial match using LEFT() in combination with MATCH

    This is getting pretty convoluted. A lot of mixed data.
    Bad enough some text some number.
    But throw in some 5 digits and some 6 digits..
    This may be getting to be too much.

    Can you post a sample book with enough rows to show all possible data types?
    Replace any personal confidential data with bogus data.
    And enter by hand the results you expect to recieve.

  14. #14
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    No problem, I've put all the data I need to check against into a clean file.

    Depending on the input in Col D, I want to crosscheck with Col A and give the material in Col B as output.

    Book1.xls

    I Agree I need to get a grip on the data in Col A to begin with. Inconsistant data is a nightmare.
    Last edited by bluerabbit; 04-15-2013 at 10:53 AM.

  15. #15
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Partial match using LEFT() in combination with MATCH

    Your first D2 example was 11011-2, when you have 6 digits is that everything before the dash? If so then try using this fomula to pull everything before the dash (or the whole thing if there is no dash):

    =LEFT(D2,FIND("-",D2&"-")-1)

    then incorporate that in your INDEX/MATCH formula like this

    =INDEX($A$2:$B$1000,MATCH(LEFT(D2,FIND("-",D2&"-")-1),INDEX($A$2:$A$1000&"",0),0),2)

    That will cope with both text and numbers because it leaves the lookup value as text but converts the lookup range to text to match (and the lookup value can be any number of characters)
    Audere est facere

  16. #16
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Partial match using LEFT() in combination with MATCH

    Given that sample book, why are we bothering with the left function at all??
    Why not just
    =VLOOKUP(D2,A:B,2,FALSE)

  17. #17
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    Quote Originally Posted by daddylonglegs View Post
    Your first D2 example was 11011-2, when you have 6 digits is that everything before the dash? If so then try using this fomula to pull everything before the dash (or the whole thing if there is no dash):
    That is correct, al the data after the " -" isn't relavant as it doesn' t change anything about the material to use.

    Your suggestion made al my problems go away. That realy did the trick. I'll now go and spend some time to figure out what you exactly did.
    I can't thank you enough for your help. To bad you don't live nearby otherwise I'd buy you a pint or two (maybe even three )
    Last edited by bluerabbit; 04-15-2013 at 11:24 AM.

  18. #18
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    Quote Originally Posted by Jonmo1 View Post
    Given that sample book, why are we bothering with the left function at all??
    Why not just
    =VLOOKUP(D2,A:B,2,FALSE)
    Because when I try to match with 32010-2x I get an error. It's my bad though, because I forgot to include some search possibilities.

    This is more like it:

    For Col D (search):
    11011
    11072
    31015
    32010-2x
    151071-2x
    1131X
    1302G
    91071
    11055

    Am I pushing it if I tried to replace the D2 with the ROW()? The reason for this, is that later on I'll be using the search formula in aprox 100 rows and I don't want output to get messed up if new rows are inserted inbetween existing rows.
    Last edited by bluerabbit; 04-15-2013 at 11:32 AM.

  19. #19
    Valued Forum Contributor Lemice's Avatar
    Join Date
    04-13-2013
    Location
    Somewhere.
    MS-Off Ver
    Excel 2016
    Posts
    696

    Re: Partial match using LEFT() in combination with MATCH

    If everything after the "-" is irrelevant, you can use =LEFT(D2,FIND("-",D2&"-")-1) as daddylonglegs suggested.
    It's saying "Check string D2, start from the left, number of character is from first letter to the first dash from the left, then minus one (the dash)"
    And there we go, a string consists of everything before the dash.
    Just to make sure, you won't have anymore dashes right, because it will stop at the first dash.
    Last edited by Lemice; 04-15-2013 at 11:35 AM. Reason: grammar
    (copy pasta from Ford)
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools

    Regards,
    Lem

  20. #20
    Registered User
    Join Date
    04-12-2013
    Location
    holland
    MS-Off Ver
    Excel 2003
    Posts
    12

    Re: Partial match using LEFT() in combination with MATCH

    Well, I've got it up and running. Thank you all for helping a newbee in Excel uit of a tight spot
    I think I can say that I've learnt a lot the las few days. I hope some day I'll be able to come up with different formulas as easy as you guys do

+ 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