Code VBA to find unique values between 2sheets.

lotto009

New Member
Joined
Sep 19, 2012
Messages
23
Dear all
How I edit code VBA to find unique values between 2sheets.
Step
1. I have value at Worksheets ("sheet2"). Range. ("J2: J300") this is input
2. Bring value to compare to reference-Worksheets ("sheet1"). Range ("R3: R500")
3. If a duplicate value with reference-Worksheets ("sheet1"). Range ("R3: R500") no need.
4. Not duplicate value with reference-Worksheets ("sheet1"). Range ("R3: R500") keep value in the reference sheet and bring output value to Worksheets ("sheet2"). Range. ("P2: P500")
because I need value only reference-Worksheets ("sheet1"). Range ("R3: R500")
Thank you I show to code att and file excel in my link


VBA Code:
Sub Button1_Click()
    Dim Na As Long, Nc As Long, Ne As Long
    Dim i As Long
    Na = Worksheets("sheet1").Cells(Rows.Count, "R").End(xlUp).Row
    Nc = Worksheets("sheet2").Cells(Rows.Count, "J").End(xlUp).Row
    Ne = 1

    For i = 1 To Na
        Cells(Ne, "P").Value = Cells(i, "R").Value
        Ne = Ne + 1
    Next i
    For i = 1 To Na
        Cells(Ne, "P").Value = Cells(i, "J").Value
        Ne = Ne + 1
    Next i

    Range("P:P").RemoveDuplicates Columns:=1, Header:=xlNo
   
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Try this:

VBA Code:
Dim Na As Long, Nc As Long, Ne, i As Long
    Dim A
    
    Na = Worksheets("sheet1").Cells(Rows.Count, "J").End(xlUp).Row
    Nc = Worksheets("sheet2").Cells(Rows.Count, "R").End(xlUp).Row
    Ne = 2
    
    For i = 3 To Nc
        On Error Resume Next
        A = Sheets("sheet1").Range("J2:J" & Na).Find(Sheets("sheet2").Range("R" & i))
        If A Then
        Else
            Sheets("sheet1").Range("P" & Ne) = Sheets("sheet2").Range("R" & i)
            Ne = Ne + 1
        End If
    Next i
 
Upvote 0
Solution
Try this:

VBA Code:
Dim Na As Long, Nc As Long, Ne, i As Long
    Dim A
   
    Na = Worksheets("sheet1").Cells(Rows.Count, "J").End(xlUp).Row
    Nc = Worksheets("sheet2").Cells(Rows.Count, "R").End(xlUp).Row
    Ne = 2
   
    For i = 3 To Nc
        On Error Resume Next
        A = Sheets("sheet1").Range("J2:J" & Na).Find(Sheets("sheet2").Range("R" & i))
        If A Then
        Else
            Sheets("sheet1").Range("P" & Ne) = Sheets("sheet2").Range("R" & i)
            Ne = Ne + 1
        End If
    Next i
Dear
As above not work kindly advisor
after edit
VBA Code:
Sub Button1_Click()
   Dim Na As Long, Nc As Long, Ne, i As Long
   Dim A
    
    Na = Worksheets("sheet1").Cells(Rows.Count, "r").End(xlUp).Row 'refer
    Nc = Worksheets("sheet2").Cells(Rows.Count, "j").End(xlUp).Row 'input
    Ne = 2
    
    For i = 3 To Nc
        On Error Resume Next
        A = Sheets("sheet1").Range("r2:r" & Na).Find(Sheets("sheet2").Range("j" & i))
        If A Then
        Else
            Sheets("sheet2").Range("P" & Ne) = Sheets("sheet2").Range("j" & i)
            Ne = Ne + 1
        End If
    Next i

    
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,628
Messages
6,120,618
Members
448,973
Latest member
ChristineC

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