Consulting

Results 1 to 7 of 7

Thread: Solved: Help with Lookup Function

  1. #1
    VBAX Regular
    Joined
    Jul 2007
    Posts
    18
    Location

    Solved: Help with Lookup Function

    I am trying to use a simple excel function here ?Lookup?.
    I have used this function several times; however in this case I am having a lot of problems.

    I have two sheets. Sheet 1 has ?T-Codes? and Sheet 2 has ?T-codes? and ?VIN#?s?.
    I have to basically pull the respective VIN #?s from sheet 2 for the T-Codes on sheet 1.

    Formula on sheet 1. Cell A1
    LOOKUP(E2,Sheet2!B:B,Sheet2!A:A)

    However the problem I am running into is; if Lookup cannot find the T-Code on sheet 2, it gives me some arbitrary VIN# value.
    Also sometimes there will be a T-Code on Sheet 2, but lookup will be confused and give me a zero value on sheet 1.

    I have arranged columns on both the sheets in an ascending manner and the category defined for all the columns is ?General?

    The advanced problem I have is I am trying to create a template, so these numbers will change on an everyday basis and lookup has to work efficiently each time.
    The data is going to be extensive and I cannot afford to give a wrong VIN# to a T-code.

    Please refer to the attachment

    Is there some other way I can handle this situation?

    Thank you all

  2. #2
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Try this formula in A2 and copy down:

    =IF(COUNTIF(Sheet2!B:B,E2)=0,"",INDEX(Sheet2!A:A,MATCH(Sheet1!E2,Sheet2!B:B ,0)))

    HTH
    Rory

  3. #3
    VBAX Regular
    Joined
    Jul 2007
    Posts
    18
    Location
    Awesome, it works. I hope its robust.
    Can you please explain me what you exactly did?
    How does the Index and match function work?

  4. #4
    VBAX Master
    Joined
    Jun 2007
    Location
    East Sussex
    Posts
    1,110
    Location
    Sure. The COUNTIF part checks to see if there is a match (if not, the MATCH function would return a #N/A error). If there isn't it returns "", otherwise we get to the INDEX/MATCH part.
    The MATCH function finds the position within column B on sheet2 where the value in E2 on Sheet1 can be found. The 0 as the third argument says that we only want an exact match, which avoids the problem you had with LOOKUP. The INDEX function then retrieves the value from that row in column A on Sheet2.
    You can't use VLOOKUP which would have been the alternative solution because VLOOKUP will only look values up in the first column of the lookup table and your lookup values are in the second column.

    HTH
    Rory

  5. #5
    VBAX Regular
    Joined
    Jul 2007
    Posts
    18
    Location
    Thanks a Lot!!

  6. #6
    VBAX Regular
    Joined
    Jul 2007
    Posts
    18
    Location
    Another question!!
    What if i want the formula to generate only if there are values in the other cell.
    Eg:

    Column A Column B
    1 Formula Value
    2 Formula Value
    3 Formula Value
    4 No Formula No Value

    Since the values are dynamic, the number of rows will change on a regular basis.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    =IF(OR(B2="",NOT(COUNTIF(Sheet2!B:B,E2))),"",INDEX(Sheet2!A:A,MATCH(Sheet1! E2,Sheet2!B:B,0)))
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •