Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to find duplicate or unique values in two columns of two sheets?

Maybe compare two ranges in the same worksheet and find out the duplicate or unique values is easy for most of you, but if the two ranges are in the two different worksheets, how can you quickly find out the duplicate and unique values in these two ranges? This tutorial will introduce some quick cuts for you.

Compare two same header columns in two spread sheets with formula in Excel

Compare two ranges in two spread sheets with VBA

Compare two ranges in two spread sheets with Kutools for Excel


arrow blue right bubble Compare two same header columns in two spread sheets with formula in Excel


With the formula in Excel, you can compare two same header columns as shown as below on different sheets and find the duplicate and unique values:

1. Typing this formula =COUNTIF(Sheet1!$A:$A, A1) in a blank cell which is adjacent to the range in Sheet 3. See screenshot:

2. Press Enter button on the keyboard, and then drag the fill handle to fill the range you want to compare with the range in Sheet 1. (Number Zero means duplicate values in two ranges, and Number 1 means unique values in Sheet3, but not in Sheet1)

Tips:

1.This formula only can compare two columns with the same header in two spreadsheets.

2. If you want to find the unique values in Sheet1, but not in Sheet3, you need to enter the above formula =COUNTIF(Sheet3!$A:$A, A1) into Sheet1.


arrow blue right bubble Compare two ranges in two spread sheets with VBA

1. Hold ALT button and press F11 on the keyboard to open a Microsoft Visual Basic for Application window.

2. Click Insert > Module, and copy the VBA into the module.

VBA: Compare two ranges in two spreadsheets

Sub CompareRanges()
'Update 20130815
Dim WorkRng1 As Range, WorkRng2 As Range, Rng1 As Range, Rng2 As Range
xTitleId = "KutoolsforExcel"
Set WorkRng1 = Application.InputBox("Range A:", xTitleId, "", Type:=8)
Set WorkRng2 = Application.InputBox("Range B:", xTitleId, Type:=8)
For Each Rng1 In WorkRng1
    rng1Value = Rng1.Value
    For Each Rng2 In WorkRng2
        If rng1Value = Rng2.Value Then
            Rng1.Interior.Color = VBA.RGB(255, 0, 0)
            Exit For
        End If
    Next
Next
End Sub

3. Click Run button or press F5 to run the VBA.

4. There is a dialog displayed on the screen, and you should select one range you want to compare with. See screenshot:

5. Click Ok and another dialog is displayed for you to select the second range. See screenshot:

6. Click Ok, and the duplicate values both in Range A and in Range B are highlighted with the red background in Range A. See screenshot:

Tips: With this VBA, you can compare two ranges both in the same and different worksheets.


arrow blue right bubble Compare two ranges in two spread sheets with Kutools for Excel

If formula is not convenient for you, and VBA is difficult for you, you can have a try on Kutools for Excel's Compare Ranges function.

Kutools for Excel includes more than 300 handy Excel tools. Free to try with no limitation in 30 days. Get it Now

Please apply Compare Ranges function by clicking Kutools > Compare Ranges. See screenshot:

Compare with two columns in different sheets:

1. Click Kutools > Compare Ranges, a dialog is displayed in the screen. See screenshot:

2. Specify the ranges and rules, do as follows:

Leave Compare in same range uncheck, and select two ranges by clicking Range A and Range B, see screenshot:

Specify the same values or different values you want to find in the drop-down box under Rules;

3. Click Ok. A pop-up dialog tells you the same values are selected.

4. Click Ok button in the pop-up dialog. The same values between two ranges are selected in Range A.

Compare two ranges in spread worksheets

If you have two ranges in two spreadsheets as shown as below, and you want to compare them and find out the different values, you can do as follows:

1. Click Kutools > Compare Ranges, a dialog is displayed in the screen.

2. Specify the ranges and rules, do as follows:

Leave Compare in same range uncheck, and select two ranges by clicking Range A and Range B;

Specify the different values you want to find in the drop-down box under Rules;

Check My data has headers in Options section;

3. Click Ok. A pop-up dialog tells you the same values are selected.

4. Click Ok button in the pop-up dialog. The different values in Range A are selected. See screenshot:

If you want to find out the different values in Range B of Sheet2, you need to swap the two ranges.

Compare Ranges function also can compare ranges in the same sheet. Click here to know more about Compare Ranges.


Relative articles:


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Nurul Islam Chowdhur · 1 years ago
    This is good. It highlights only one sheet duplicate item. But I need to highlight both the sheet where duplicate items are


    • To post as a guest, your comment is unpublished.
      Chris · 1 years ago
      You need to add a second command inside the THEN statement.


      Try this;

      Sub CompareRanges()
      'Update 20130815
      Dim WorkRng1 As Range, WorkRng2 As Range, Rng1 As Range, Rng2 As Range
      xTitleId = "Enter Range for Comparison"
      Set WorkRng1 = Application.InputBox("Range A:", xTitleId, "", Type:=8)
      Set WorkRng2 = Application.InputBox("Range B:", xTitleId, Type:=8)
      For Each Rng1 In WorkRng1
      rng1Value = Rng1.Value
      For Each Rng2 In WorkRng2
      If rng1Value = Rng2.Value Then
      Rng1.Interior.Color = VBA.RGB(255, 0, 0)
      Rng2.Interior.Color = VBA.RGB(255, 0, 0)
      Exit For
      End If
      Next
      Next
      End Sub
  • To post as a guest, your comment is unpublished.
    SM3117 · 2 years ago
    Hi,morning,

    I need to look up for a range of values in all the available worksheets, and in case there is any matches, then colour the value on the original range.

    I have tried the following code:

    Sub Compare3()


    Dim WorkRng1 As Range
    Dim WorkRng2 As Range
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim DataRange As Range
    Dim ws As Worksheet


    xTitleId = "Buscar coincidencias"


    Set WorkRng1 = Application.InputBox("Seleccionar equipos con cambios:", xTitleId, "", Type:=8)
    Set WorkRng2 = Range("B1" & LastRow)


    For Each Rng1 In WorkRng1
    rng1Value = Rng1.Value
    For Each ws In ActiveWorkbook.Worksheets
    For Each Rng2 In WorkRng2
    If rng1Value = Rng2.Value Then
    Rng1.Interior.Color = VBA.RGB(200, 250, 200)
    Exit For
    End If
    Next
    Next
    Next


    End Sub

    But it does not make any changes when there are matches.... Could someone help??
    Many thanks,
    Have a good day
  • To post as a guest, your comment is unpublished.
    SM3117 · 2 years ago
    Hi,

    I need to look up for a range of values in all the available worksheets, and in case there is any matches, then colour the value on the original range.

    I have tried the following code:

    Sub Compare3()


    Dim WorkRng1 As Range
    Dim WorkRng2 As Range
    Dim Rng1 As Range
    Dim Rng2 As Range
    Dim DataRange As Range
    Dim ws As Worksheet


    xTitleId = "Buscar coincidencias"


    Set WorkRng1 = Application.InputBox("Seleccionar equipos con cambios:", xTitleId, "", Type:=8)
    Set WorkRng2 = Range("B1" & LastRow)


    For Each Rng1 In WorkRng1
    rng1Value = Rng1.Value
    For Each ws In ActiveWorkbook.Worksheets
    For Each Rng2 In WorkRng2
    If rng1Value = Rng2.Value Then
    Rng1.Interior.Color = VBA.RGB(200, 250, 200)
    Exit For
    End If
    Next
    Next
    Next


    End Sub

    But it does not make any changes when there are matches.... Could someone help??
    Many thanks,
    Have a good day
  • To post as a guest, your comment is unpublished.
    Neelakshi Nehru · 4 years ago
    Hi, the code is working but the entire range is getting highlighted even if there are no duplicate values. Help!
  • To post as a guest, your comment is unpublished.
    tim · 4 years ago
    how to compare datas between two different excel sheets?