SOLVED

Excel help pulling data to a new sheet by comparing data for matches in 2 other work sheets

Brass Contributor

Hi all,

I have a workbook with 2 worksheets, Sheet1 and Sheet2.  

 

I need to lookup values in Sheet1, Col E (Employee-E) and Col H (Ded-Code-E)  and find matches in Sheet2, Col C (EdmEmployee) and Col D (EdmDedCode).  If I find a match, I want to copy the entire row over to new Sheet3.

 

I've uploaded test spreadsheet with a few rows for reference.

 

I have a VB macro in there, but don't know how to edit it to do what I want.  Pretty new to Excel and macros, so I'm a bit over my head here!

 

Thanks in advance!!

 

Have a great day:)

 

Rufus 

12 Replies

@Rufus_Bertrand 

From which sheet do you want to copy a row to Sheet3? From Sheet1 or from Sheet2?

HI Hans,

I want to return row from Sheet2.

thanks!
Amazing! That is what I was looking for?

Mind if i ask a couple of questions about the code?

thanks,
Rufus
ok, first, thank you very much for your help!!

I have a question about the following code:
' Create dictionary of unique helper values
Set dct = CreateObject(Class:="Scripting.Dictionary")
For curRowLookup1 = 2 To lastRowLookup1
dct(lookupSheet1.Range("L" & curRowLookup1).Value) = 1
Next curRowLookup1

* this is the first time i see the 'Scripting.Dictionary' object, so you're creating an Object called 'dct', then you search Sheet1 from row=2 to last row, and assigning it the value in col 'L'. Is '1' the key associated with it?

@Rufus_Bertrand 

For the Dictionary object, see:

Dictionary object 

Excel VBA Dictionary – A Complete Guide 

The line

dct(lookupSheet1.Range("L" & curRowLookup1).Value) = 1

does one of two things:

  1. If there is no entry with key equal to lookupSheet1.Range("L" & curRowLookup1).Value, it creates a new entry with that key, whose item (value) is 1. The 1 is not important, I could have used any value. What matters if that an entry with the cell value as key is created.
  2. If there is already an item with key equal to lookupSheet1.Range("L" & curRowLookup1).Value, its item is set to 1, so effectively nothing happens.
So in answer 1, the actual key is the value in column 'L', like '129|6000' for first record on Sheet1?
And in answer 2, if there is already a value in dct equal to '129|6000', it returns a '1'?
best response confirmed by Rufus_Bertrand (Brass Contributor)
Solution
Great!
I'll check out the links you provided to learn more about Dictionary object usage.

thanks so much for your help Hans!!
Have a great day:)
1 best response

Accepted Solutions
best response confirmed by Rufus_Bertrand (Brass Contributor)
Solution
Great!
I'll check out the links you provided to learn more about Dictionary object usage.

thanks so much for your help Hans!!
Have a great day:)

View solution in original post