Comparing two worksheets to find the match including the duplicates

nike

Board Regular
Joined
Feb 12, 2008
Messages
113
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have two worksheets that i'd like to compare the IDs to find the match including the duplicates. IDs in Sheet1 will be compared to a queried data with IDs in Sheet2. If it's possible if there's a code that can make this compare, create a tab labeled Matched including the duplicates, another tab labeled No match IDs from Sheet1 that didn't match to Sheet2, and lastly a tab that matched the IDs but has no duplicate IDs. Thank you!

Here's an example of the worksheets.

SHEET1
IDS

*A
1ID #
20188370
30283750
40299950
50316740
60416500
70538620
80543610
90554670
100556410
110572240
120724270

<COLGROUP><COL style="WIDTH: 30px; FONT-WEIGHT: bold"><COL style="WIDTH: 64px"></COLGROUP><TBODY>
</TBODY>


Excel tables to the web >> Excel Jeanie Html" target="_blank"> Excel Jeanie HTML 4

SHEET2

TOAD QUERY

*ABCDEFGHIJKLMNOPQ
1ID #YRTOSBRCOTERMSTATUSWAGE 1WAGE 2WAGE_3TAX 1TAX 2YTD YTD 2YTD 3YTD 4YTD 5
20188370060000046S26 A33225.5022136.5000032503.0222136.50
30188370060000046A01A10.2010.2000025332253320
40188370060000047A02A2.502.5000023.223.20
50188370060000048A01A23202320000221122110
60188370060000045A01A212302123000022235222350
702837501333333BSJ0X A0000000000
802837501333333CSJ0X A0000000000
902999501433333729HQ A37961.53095000256.50037189.3995000
1003167401433333RWMVG A11538.42095000256.50011538.4295000
110416500143333346SCB A0000000000
1205386201300000A0DN3 FI0000000000
1305386201300000A0DN3 FI0000000000
1405386201300000A0DN3 FI0000000000
150543610130000070WWD A5949.32000000000
160543610130000070WWD A5949.32000000000
1705546701003300STNML6A0000000000
180556410110333372MSV A0000000000
190572240103333380AQD I00000*****
2007242701433333R2I34 A3333.3403333.34090003333.343333.340
210724610 * 1100333153ML A388389.810251990.8606803.7500394163.06251990.860
220724610 * * 143333315E2X A17561.48095000256.50017561.4895000
230724680 *1499999159O2 TA00000*****
24APPLFOR042546583 140333346FH2 A00000*****
25APPLFOR043103733 093000046C4C A15759000000000

<COLGROUP><COL style="WIDTH: 30px; FONT-WEIGHT: bold"><COL style="WIDTH: 62px"><COL style="WIDTH: 23px"><COL style="WIDTH: 47px"><COL style="WIDTH: 28px"><COL style="WIDTH: 51px"><COL style="WIDTH: 41px"><COL style="WIDTH: 64px"><COL style="WIDTH: 81px"><COL style="WIDTH: 67px"><COL style="WIDTH: 81px"><COL style="WIDTH: 41px"><COL style="WIDTH: 74px"><COL style="WIDTH: 35px"><COL style="WIDTH: 46px"><COL style="WIDTH: 79px"><COL style="WIDTH: 79px"><COL style="WIDTH: 41px"></COLGROUP><TBODY>
</TBODY>


Excel tables to the web >> Excel Jeanie Html" target="_blank"> Excel Jeanie HTML 4
 
Hi hiker95,

I uploaded an unprotected workbook (i hope i did it right) https://app.box.com/files/0/f/0 The worksheet names won't have a single trailing space character, i kind put it there by accident when renaming the tab. Also this workbook will always have these tab names. Thanks!
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
nike,

Your link is asking for a user name/e-mail and password.

Please try again.

You can upload your workbook to Box Net,

sensitive data changed

mark the workbook for sharing

and provide us with a link to your workbook.
 
Upvote 0
nike,

I can not work with your latest workbook:
1. locked cells
2. two worksheet names contain a trailing space character

So, I copied raw data from your earlies screenshots, and, used the new worksheets naming convention.


Sample two raw data wowrksheets:

Excel Workbook
A
1ID #
2188370
3283750
4299950
5316740
6416500
7538620
8543610
9554670
10556410
11572240
12724270
13123456
14254689
15212542
16254686
17
IDS


Excel Workbook
ABCDEFGHIJKLMNOPQ
1ID #YRTOSBRCOTERMSTATUSWAGE 1WAGE 2WAGE_3TAX 1TAX 2YTDYTD 2YTD 3YTD 4YTD 5
21883706046S26A33225.5022136.5000032503.0222136.50
31883706046A01A10.2010.2000025332253320
41883706047A02A2.502.5000023.223.20
51883706048A01A23202320000221122110
61883706045A01A212302123000022235222350
72837501333333BSJ0XA0000000000
82837501333333CSJ0XA0000000000
92999501433333729HQA37961.53095000256.50037189.3995000
103167401433333RWMVGA11538.42095000256.50011538.4295000
11416500143333346SCBA0000000000
12538620130A0DN3FI0000000000
13538620130A0DN3FI0000000000
14538620130A0DN3FI0000000000
1554361013070WWDA5949.32000000000
1654361013070WWDA5949.32000000000
17554670103300STNML6A0000000000
1855641011333372MSVA0000000000
19572240103333380AQDI00000*****
207242701433333R2I34A3333.3403333.34090003333.343333.340
210724610 *11333153MLA388389.810251990.8606803.7500394163.06251990.860
220724610 * *143333315E2XA17561.48095000256.50017561.4895000
230724680 *1499999159O2TA00000*****
24APPLFOR04254658314333346FH2A00000*****
25APPLFOR04310373393000046C4CA15759000000000
26
TOAD QUERY



After the macro in the three other worksheets:

Excel Workbook
ABCDEFGHIJKLMNOPQR
1SHEET1 ID#ID #YRTOSBRCOTERMSTATUSWAGE 1WAGE 2WAGE_3TAX 1TAX 2YTDYTD 2YTD 3YTD 4YTD 5
21883701883706046S26A33225.5022136.5000032503.0222136.50
31883701883706046A01A10.2010.2000025332253320
41883701883706047A02A2.502.5000023.223.20
51883701883706048A01A23202320000221122110
61883701883706045A01A212302123000022235222350
72837502837501333333BSJ0XA0000000000
82837502837501333333CSJ0XA0000000000
9538620538620130A0DN3FI0000000000
10538620538620130A0DN3FI0000000000
11538620538620130A0DN3FI0000000000
1254361054361013070WWDA5949.32000000000
1354361054361013070WWDA5949.32000000000
14
MATCHED ALL


Excel Workbook
ABCDEFGHIJKLMNOPQR
1SHEET1 ID#ID #YRTOSBRCOTERMSTATUSWAGE 1WAGE 2WAGE_3TAX 1TAX 2YTDYTD 2YTD 3YTD 4YTD 5
22999502999501433333729HQA37961.53095000256.50037189.3995000
33167403167401433333RWMVGA11538.42095000256.50011538.4295000
4416500416500143333346SCBA0000000000
5554670554670103300STNML6A0000000000
655641055641011333372MSVA0000000000
7572240572240103333380AQDI00000*****
87242707242701433333R2I34A3333.3403333.34090003333.343333.340
9
MATCHED NO DUP


Excel Workbook
ABCDEFGHIJKLMNOPQR
1SHEET1 ID#ID #YRTOSBRCOTERMSTATUSWAGE 1WAGE 2WAGE_3TAX 1TAX 2YTDYTD 2YTD 3YTD 4YTD 5
2123456
3254689
4212542
5254686
6
NO MATCH




Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub CompareIDS()
' hiker95, 04/19/2014, ME772155
Dim wids As Worksheet, wtq As Worksheet, wma As Worksheet, wmnd As Worksheet, wnm As Worksheet
Dim c As Range, irng As Range, n As Long, nr As Long
Application.ScreenUpdating = False
Set wids = Sheets("IDS")
Set wtq = Sheets("TOAD QUERY")
Set wma = Sheets("MATCHED ALL")
Set wmnd = Sheets("MATCHED NO DUP")
Set wnm = Sheets("NO MATCH")

With wids
  For Each c In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
    
    '*** "TOAD QUERY" = wtq
    Set irng = wtq.Columns(1).Find(c, LookAt:=xlWhole)
    If irng Is Nothing Then
    
      '*** "NO MATCH" = wnm
      nr = wnm.Cells(wnm.Rows.Count, "A").End(xlUp).Row + 1
      c.Copy wnm.Range("A" & nr)
    
    ElseIf Not irng Is Nothing Then
      n = Application.CountIf(wtq.Columns(1), c)
      
      '*** "MATCHED NO DUP" = wmnd
      If n = 1 Then
        nr = wmnd.Cells(wnm.Rows.Count, "A").End(xlUp).Row + 1
        c.Copy wmnd.Range("A" & nr)
        wtq.Range("A" & irng.Row).Resize(, 17).Copy wmnd.Range("B" & nr).Resize(, 17)
        Application.CutCopyMode = False
        
      '*** "MATCHED ALL" = wma
      ElseIf n > 1 Then
        nr = wma.Cells(wnm.Rows.Count, "A").End(xlUp).Row + 1
        c.Copy wma.Range("A" & nr).Resize(n)
        wtq.Range("A" & irng.Row).Resize(n, 17).Copy wma.Range("B" & nr).Resize(n, 17)
        Application.CutCopyMode = False
      End If
    End If
    Set irng = Nothing
  Next c
End With
wma.Columns.AutoFit
wmnd.Columns.AutoFit
wnm.Columns.AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CompareIDS macro.
 
Upvote 0
nike,

So, I copied raw data from your earlies screenshots, and, used the new worksheets naming convention - without trailing space characters.


Here is a screenshot showing where all the data came from, and, where it was copied to:

Excel Workbook
ABCDEFGHI
1IDSTOAD QUERYMATCHED ALLMATCHED NO DUPNO MATCH
2ID # ID #SHEET1 ID#SHEET1 ID#SHEET1 ID#
3188370188370188370
4188370188370
5188370188370
6188370188370
7188370188370
8283750283750283750
9283750283750
10299950299950299950
11316740316740316740
12416500416500416500
13538620538620538620
14538620538620
15538620538620
16543610543610543610
17543610543610
18554670554670554670
19556410556410556410
20572240572240572240
21724270724270724270
22123456123456
23254689254689
24212542212542
25254686254686
260724610 *
270724610 * *
280724680 *
29APPLFOR042546583
30APPLFOR043103733
31
Results
 
Last edited:
Upvote 0
Hi hiker95,

Thank you so much for taking the time to help me with this! I just have one issue with the macro. I've tested it and it works great, but for "MATCHED ALL" tab, it's not including the IDs that only has one match, basically it's those IDs that's on tab "MATCHED NO DUP". If possible if even it has only one match to include in the "MATCHED ALL" tab. I really appreciate your help!
 
Upvote 0
nike,

basically it's those IDs that's on tab "MATCHED NO DUP". If possible if even it has only one match to include in the "MATCHED ALL" tab.

Then why do you have a "MATCHED NO DUP" worksheet?
 
Upvote 0
Hi,

It's because the "MATCHED ALL" will all have to be totaled their dollar amount including the IDs that matched and it's duplicates. The "MATCHED NO DUP" will go to another department to be added to their database, which has to be the only unique Ids that matched, the reason why i need to be separate them.
 
Upvote 0
nike,

One last try.

What is your logic for putting information into worksheet "MATCHED NO DUP"?
 
Upvote 0
Hi,

For "MATCHED NO DUP", (this is basically for the other department) The logic is to have a unique ID that matched my department's query. It doesn't really matter the information that is attached to this ID. I've asked this question too to the other department, but i was told they need only the unique ID that matched or it'll be alot of manual work sorting out only the unique ID. This report that i'm working on has thousands of IDs and it's monthly report. Basically for my department all i need is the Matched all (including all the duplicates and unique Ids that matched) and No match. If it's too much work, we can just eliminate the "MATCHED NO DUP". Sorry for all the troubles.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,376
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top