+ Reply to Thread
Results 1 to 8 of 8

VBA - Lookup with multiple conditions/criteria

  1. #1
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,101

    VBA - Lookup with multiple conditions/criteria

    Hello Experts,

    Just wondering if there's a way to do a lookup with multiple criteria in 2 different workbooks.

    i.e. wb1 were values that I need to lookup.
    wb2 were the values where I need to lookup at.

    in excel formula simple term, vlookup(wb1, wb2, col-index, false)

    That being said, I have named ranged in wb2, for easy reference.

    Thus, ASSUMING that I have CustName at wb1 and wb2, with additional info at wb2 such as birth date and gender, how would i go about checking and returning the value (country) for me.

    example:
    wb1 - Dom Lian (custName)
    - Male (Gender)
    - Jan 1, 1990 (DOB)

    wb2 - Dom Lian (custName)
    - Male (Gender)
    - Jan 1, 1990 (DOB)
    - USA (Country)

    - Dom Lian (custName)
    - Male (Gender)
    - Jan 1, 2012 (DOB)
    - USA (Country)



    I could do this in Excel with vlookup array formula...but do not know how to do this on VBA.

    I tried to record the formulated macro...but it gives me a type mismatch.

    Please Login or Register  to view this content.

  2. #2
    Registered User
    Join Date
    01-01-2005
    MS-Off Ver
    2007
    Posts
    368

    Re: VBA - Lookup with multiple conditions/criteria

    Hi,

    I attached a wb2 with a table named "Table1".

    Put this code in wb1:

    Please Login or Register  to view this content.
    Regards,
    Elio Fernandes
    Attached Files Attached Files
    Last edited by efernandes67; 04-30-2013 at 06:31 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,101

    Re: VBA - Lookup with multiple conditions/criteria

    Thanks efernandes!

    Although I've not tested it out yet, but by looking at the code, you've specified custName, Geneder and DOB.

    I'm looking for a more "dynamic" where it'll be looking at each row...visualize it like a vlookup.

    Yet that being said, I'll definitely give a try...and will update it when I'm back at the office.

  4. #4
    Registered User
    Join Date
    01-01-2005
    MS-Off Ver
    2007
    Posts
    368

    Re: VBA - Lookup with multiple conditions/criteria

    Its difficult to understand what you really want without seeing your files, anyway, I tried to do something and you may change the code to suit your needs.

    I am posting wb1.xlsm, and in this file I also created a table. In the country column you will find a formula that is looking for the country in wb2 using INDEX and MATCH functions. In this way you don't need any VBA. If the wb2 is not opened then the countries will be blank.
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,101

    Re: VBA - Lookup with multiple conditions/criteria

    Efernandes,

    Thank you so much for trying to help me out.

    If I can avoid macro...I'll definitely avoid it completely!!! However, I have to do this in macro, reason being, my manager wants it to. As well, I tried to have condensed a formula, but excel won't take it, probably it's too long.

    Anyway, I've attached a sample of it...wb1 and wb2.

    wb1 is how part of the actual data is...and it contains all the "criteria" that I need to look at. Column G (currency) and column H (rate) is how it would have been with the formula.

    How I get the currency:
    1st. Look at column F (in this case, there's only 1 partner code id) - partner code id is AUSOP
    2nd. Look at column B and determine the "earliest" and "latest" date. In our data, 201012 is earliest date and 201208 is the latest date in column B.
    3rd. With the above 2 criterias, it's time that we look at wb2.
    4th. In wb2, 'Partner Rates' sheet, noticed that we have 2 AUSOP partner code in column A (colored in yellow). But the effective date and end date of these 2 are different. With the vlookup array formula, it has taken row 4 (colored green) since our earliest date in wb1 (201012) is <= effective date in wb2 of row 4, the same with our latest date in wb1 (201208) >= end date in wb2.
    5th. Since there's a match, we take column F.

    How I get the Rate:
    1st. The step is the same as above where the code has to determine which row to take after meeting the criteria (partner code, earliest date <= effective date, and latest date>= end date)
    2nd. on wb1 at column C, we determine if it's a "GPRS, SMS or Voice".
    3rd. If it's GPRS, we return the rate (column O of wb2) in the determined row on wb2 (in our example, row 4).
    4th. If it's SMS, we return the rate (column N of wb2) in the determined row on wb2 (in our example, row 4).

    5th. Voice would be a little troublesome, since it has to look at the country (column E) in wb1, then look up at wb2 "Special Destination" Sheet to determine if it's 'YES' and then return the rate value (column J of wb2 "Partner Rates" sheet). At this point, I'll ignore this for a while and try to do it myself.

    PS: wb2 has named ranged.

    Below is part of the code where I try to do a multiple search criteria...but just don't know how to.

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by dluhut; 05-01-2013 at 10:42 AM.

  6. #6
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,101

    Re: VBA - Lookup with multiple conditions/criteria

    Would appreciate if any excel vba experts could help me out.

  7. #7
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,101

    Re: VBA - Lookup with multiple conditions/criteria

    Anyone help??? I'm really stuck here.

  8. #8
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,101

    Re: VBA - Lookup with multiple conditions/criteria

    Bring Up My Thread!!!

+ 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