Compare two sheets using match/find

Azurai

New Member
Joined
Feb 16, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi all..

Glad to know this forum..
I have problem with my macro excel.

I want to compare 2 sheets (1st Semester & 2nd Semester), the result is in the Sheet Output. My problem is there is possibilities that there are some values have duplicates. For example, there are 2 "Budi" Grade "7" in Sheet1 and Sheet2 but have different score.
Currently, I am using find codes to match the row from Sheet1 to Sheet2 with the condition if the row have the same "Name" and "Grade", they will be on the same row in the Sheet Output. The problem is if there is duplicate value of "Name" and "Grade", like the case of "Budi" Grade "7", the first match is being overwrite with the second match. I want the result is exactly just like in the Sheet Output without deleting any duplicates.

Here is my data:
Book1.xlsm
ABCDE
1NameGradeMath BiologyPhysics
2Ammar783387
3Agus8116424
4Budi762218
5Budi731775
6Budi8578330
7Budi9437967
8Bambang757672
9Bambang848275
10Bambang9839668
11Eli8953095
12Franda8319992
13Gladisya782236
14Gladisya9435352
15Haris8347386
16Lina9821443
17Zoey897078
1st Semester

Book1.xlsm
ABCDE
1NameGradeMath BiologyPhysics
2Ammar7226684
3Budi7218363
4Budi730778
5Budi8616631
6Budi9812092
7Bambang7237089
8Bambang842899
9Bambang9285064
10Eli776513
11Eli838664
12Franda8982689
13Gladisya7989875
14Gladisya994334
15Lina9204610
16Zoey8685663
2nd Semester

Book1.xlsm
ABCDEFGHIJKL
1NameGradeMath BiologyPhysicsNameGradeMath BiologyPhysics
2Ammar783387Ammar7226684
3Agus8116424
4Budi762218Budi7218363
5Budi731775Budi730778
6Budi8578330Budi8616631
7Budi9437967Budi9812092
8Bambang757672Bambang7237089
9Bambang848275Bambang842899
10Bambang9839668Bambang9285064
11Eli776513
12Eli8953095Eli838664
13Franda8319992Franda8982689
14Gladisya782236Gladisya7989875
15Gladisya9435352Gladisya994334
16Haris8347386
17Lina9821443Lina9204610
18Zoey897078Zoey8685663
Output

Thank you for your help. I am really appreciated..
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
like this ?
NameGradeMathBiologyPhysicsTable2.NameTable2.GradeTable2.MathTable2.BiologyTable2.Physics
Ammar783387Ammar7226684
Agus8116424
Budi762218Budi7218363
Budi762218Budi730778
Budi731775Budi7218363
Budi731775Budi730778
Budi8578330Budi8616631
Budi9437967Budi9812092
Bambang757672Bambang7237089
Bambang848275Bambang842899
Bambang9839668Bambang9285064
Eli8953095Eli838664
Franda8319992Franda8982689
Gladisya782236Gladisya7989875
Gladisya9435352Gladisya994334
Haris8347386
Lina9821443Lina9204610
Zoey897078Zoey8685663
Eli776513
 
Upvote 0
Hi @sandy666..

Thanks for the response..

But I want the result exactly like this, just match the row which have the same "Name" and "Grade", without changing the data from Sheet1 and Sheet2:
Book1.xlsm
ABCDEFGHIJKL
1NameGradeMath BiologyPhysicsNameGradeMath BiologyPhysics
2Ammar783387Ammar7226684
3Agus8116424
4Budi762218Budi7218363
5Budi731775Budi730778
6Budi8578330Budi8616631
7Budi9437967Budi9812092
8Bambang757672Bambang7237089
9Bambang848275Bambang842899
10Bambang9839668Bambang9285064
11Eli776513
12Eli8953095Eli838664
13Franda8319992Franda8982689
14Gladisya782236Gladisya7989875
15Gladisya9435352Gladisya994334
16Haris8347386
17Lina9821443Lina9204610
18Zoey897078Zoey8685663
Output

Thank you..
 
Upvote 0
Try this:
Copy data in both sheet to the sheet Output (to col A & H) then run this code:
VBA Code:
Sub a1124363b()
'https://www.mrexcel.com/board/threads/compare-two-sheets-using-match-find.1124363/
Dim a As Range, b As Range
Dim n As Long, i As Long
Application.ScreenUpdating = False
n = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To 2 * n
    Set a = Cells(i, "A")
    Set b = Cells(i, "H")
    If Len(a) = 0 And Len(b) = 0 Then Exit Sub
        If Len(a) <> 0 And Len(b) <> 0 Then
            If a.Value < b.Value Then
                b.Resize(1, 5).Insert
            ElseIf a.Value > b.Value Then
                a.Resize(1, 5).Insert
            ElseIf a.Value = b.Value Then
                If a.Offset(, 1).Value < b.Offset(, 1).Value Then
                b.Resize(1, 5).Insert
                ElseIf a.Offset(, 1).Value > b.Offset(, 1).Value Then
                a.Resize(1, 5).Insert
                End If
            End If
        End If
Next
Application.ScreenUpdating = True
End Sub

Book2
ABCDEFGHIJKL
1NameGradeMathBiologyPhysicsNameGradeMathBiologyPhysics
2Ammar782.8484137.864756.964003Ammar721.6446965.825884.46054
3Agus811.1440964.146123.60417
4Budi761.8300321.436117.712235Budi720.7185882.9626562.99988
5Budi730.9325376.735724.958872Budi729.507387.01368678.34777
6Budi857.4947982.5988329.55132Budi861.4695266.180931.02363
7Budi943.4629579.4211667.09739Budi981.1129220.2459491.73688
8Bambang75.17200376.182771.78864Bambang722.8152869.8197288.74699
9Bambang83.69227281.6937674.72925Bambang841.7151688.583379.382638
10Bambang982.9757796.496168.20357Bambang927.6841650.0634264.42479
11Eli76.92189165.367612.99684
12Eli895.2038530.0202294.55375Eli838.447996.04329764.0684
13Franda831.181799.1459692.15863Franda897.551625.5262389.15507
14Gladisya78.05640722.0031335.57952Gladisya797.5843498.3905974.56842
15Gladisya943.4656452.7353651.57634Gladisya993.7283432.89324.156494
16Haris834.0111272.8166685.90114
17Lina981.5454714.4440543.05643Lina920.0801346.316689.738968
18Zoey88.91153770.0526178.01206Zoey868.493455.7973762.90382
Sheet2
 
Upvote 0
Try this:
Copy data in both sheet to the sheet Output (to col A & H) then run this code:
VBA Code:
Sub a1124363b()
'https://www.mrexcel.com/board/threads/compare-two-sheets-using-match-find.1124363/
Dim a As Range, b As Range
Dim n As Long, i As Long
Application.ScreenUpdating = False
n = Range("A" & Rows.Count).End(xlUp).Row

For i = 2 To 2 * n
    Set a = Cells(i, "A")
    Set b = Cells(i, "H")
    If Len(a) = 0 And Len(b) = 0 Then Exit Sub
        If Len(a) <> 0 And Len(b) <> 0 Then
            If a.Value < b.Value Then
                b.Resize(1, 5).Insert
            ElseIf a.Value > b.Value Then
                a.Resize(1, 5).Insert
            ElseIf a.Value = b.Value Then
                If a.Offset(, 1).Value < b.Offset(, 1).Value Then
                b.Resize(1, 5).Insert
                ElseIf a.Offset(, 1).Value > b.Offset(, 1).Value Then
                a.Resize(1, 5).Insert
                End If
            End If
        End If
Next
Application.ScreenUpdating = True
End Sub

Book2
ABCDEFGHIJKL
1NameGradeMathBiologyPhysicsNameGradeMathBiologyPhysics
2Ammar782.8484137.864756.964003Ammar721.6446965.825884.46054
3Agus811.1440964.146123.60417
4Budi761.8300321.436117.712235Budi720.7185882.9626562.99988
5Budi730.9325376.735724.958872Budi729.507387.01368678.34777
6Budi857.4947982.5988329.55132Budi861.4695266.180931.02363
7Budi943.4629579.4211667.09739Budi981.1129220.2459491.73688
8Bambang75.17200376.182771.78864Bambang722.8152869.8197288.74699
9Bambang83.69227281.6937674.72925Bambang841.7151688.583379.382638
10Bambang982.9757796.496168.20357Bambang927.6841650.0634264.42479
11Eli76.92189165.367612.99684
12Eli895.2038530.0202294.55375Eli838.447996.04329764.0684
13Franda831.181799.1459692.15863Franda897.551625.5262389.15507
14Gladisya78.05640722.0031335.57952Gladisya797.5843498.3905974.56842
15Gladisya943.4656452.7353651.57634Gladisya993.7283432.89324.156494
16Haris834.0111272.8166685.90114
17Lina981.5454714.4440543.05643Lina920.0801346.316689.738968
18Zoey88.91153770.0526178.01206Zoey868.493455.7973762.90382
Sheet2

Hi.. Thank you for your script. It works for that kind of data.
I want to ask further question. What if the data is just like this:
Book1.xlsm
ABCDEFGHIJKL
1NameGradeMath BiologyPhysicsNameGradeMath BiologyPhysics
2Ammar783387Ammar783387
3Agus8116424Budi7218363
4Budi762218Budi730778
5Budi8578330Budi8616631
6Budi9437967Budi9812092
7Bambang757672Bambang7237089
8Bambang848275Bambang842899
9Bambang9839668Bambang9285064
10Budi731775Eli776513
11Eli8953095Eli838664
12Franda8319992Franda8982689
13Gladisya782236Gladisya7989875
14Haris8347386Gladisya994334
15Zoey897078Lina9204610
16Gladisya9435352Zoey8685663
Output


Is that even possible to match the row if they have same "Name" & "Grade"?
 
Upvote 0
The code works only if data is sorted by col Name and Grade (as your first example is).
So if it isn't the case then you need to sort it first then run the code.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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