A better way to find when looking for two matching criteria

  • By better, I mean more efficient. The below code works, but I can't help but feeling that it's sloppy and there is a better way to do it.


    The goal is to match up two sets of data; however, each set of data has two variables to the match. Data Set 1 consists of a column of store numbers, a column of employee IDs, and then a column of results. Data Set 2 consists of a store number column and employee ID column. An employee ID can be used in multiple stores - that is employee ID '123' can exist in each store. Therefore, when I search Data Set 2 I have to make sure what is found is for the right store. In other words, a search from Data Set 1 for employee ID '123' (in store ABC) may find employee ID '123' in Data Set 2 but for an employee in store DEF, in which case findnext has to be used to search for the next employee ID '123' until a match of ID and store are found.





    Thank you.

  • Re: A better way to find when looking for two matching criteria


    Hi Cochese


    It may be that the using the 'Dictionary' object is a better way of doing this but it would help to see the actual data and desired results.

  • Re: A better way to find when looking for two matching criteria


    Quote from dangle;636012

    Hi Cochese


    It may be that the using the 'Dictionary' object is a better way of doing this but it would help to see the actual data and desired results.


    I had never heard of the Dictionary object, but I Googled it and I'm very glad I now know about it; however, I'm not sure it solves my problem efficiently. I'm attaching a sample data spreadsheet. Thank you for the help.

  • Re: A better way to find when looking for two matching criteria


    Could you create a helper column for each data set. Helper = Employee ID & Store ID and then search through that.


    Or you could use Auto-Filter to isolate those rows that matched a given employee/store combo.


    Or you could sort each data set on Employee and then on Store to make the searching quicker.


    With someRange sorted, you know that every cell in Range(FirstCell, LastCell) contains searchTerm

    Code
    With someRange
        Set FirstCell = .Find(searchTerm, After:= .Item(.Cells.Count), SearchDirection:=xlNext)
        Set LastCell = .Find(searchTerm, After:=.Cells(1,1), SearchDirection:=xlPrevious)
    End With
  • Re: A better way to find when looking for two matching criteria


    I could work with sorting, but I just don't know if it would be a more efficient means than what I already created (and have working). I really like your idea of combining the two columns and then searching. I think that might just be the solution!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!