VBA Code for comparing two sheets and copying non duplicates

Bobsta_666

New Member
Joined
Jan 15, 2024
Messages
14
Office Version
  1. 365
Platform
  1. Windows
Hello All,
I am having trouble, I hope you can help.
I have tried a few different versions of code with no success.

Example of what I require:
Compare ("Sheet1") Range K4:K1000 with ("Sheet2") Range K4:K1000 and if there is a duplication, copy the "non-duplicates" from ("Sheet 1") Columns(B:O) to ("Sheet 2") Columns(B:O) at the bottom of the last row.

I hope its a simple code
thanks
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try:
VBA Code:
Sub CompareData()
    Application.ScreenUpdating = False
    Dim srcWS As Worksheet, desWS As Worksheet, v1 As Variant, v2 As Variant, dic As Object, i As Long
    Set srcWS = Sheets("Sheet1")
    Set desWS = Sheets("Sheet2")
    v1 = srcWS.Range("K4", srcWS.Range("K" & Rows.Count).End(xlUp)).Resize(, 14).Value
    v2 = desWS.Range("K4", desWS.Range("K" & Rows.Count).End(xlUp)).Resize(, 11).Value
    Set dic = CreateObject("Scripting.Dictionary")
    For i = LBound(v2) To UBound(v2)
        If Not dic.exists(v2(i, 1)) Then
            dic.Add v2(i, 1), i + 3
        End If
    Next i
    For i = LBound(v1) To UBound(v1)
        If dic.exists(v1(i, 1)) Then
            desWS.Cells(desWS.Rows.Count, "B").End(xlUp).Offset(1).Resize(, 14).Value = srcWS.Range("B" & i + 3).Resize(, 14).Value
        End If
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
thank you for you fast reply much appreciated.

this copies the data to the right place but only copies the duplicates over.

Is there a way it can do it with the numbers/values not matched?

I have attached sheet 1 and sheet 2 you can see Column O are the match criteria
 

Attachments

  • Sheet2.JPG
    Sheet2.JPG
    186.4 KB · Views: 11
  • Sheet1.JPG
    Sheet1.JPG
    252.2 KB · Views: 12
Upvote 0
Replace this line of code:
VBA Code:
If dic.exists(v1(i, 1)) Then
with this line:
VBA Code:
If Not dic.exists(v1(i, 1)) Then
 
Upvote 0
I have a new problem....
I have a data sheet ("Sheet 1") with raw data, I want to filter/find based on Criteria ("billy") in column ("I"), how do I copy the data within range J2:W1000 based on that (criteria ("billy")) whilst removing blanks and only copying over rows to a new sheet ("Sheet 2") that do not find a match (are not duplicates).

The idea is - I have a data sheet ("Sheet 1") with exported jobs in, I want to bring over all the new jobs to the planning sheet ("Sheet 2") that do not already exist in the planning sheet ("Sheet 2"). The new jobs from ("Sheet 1") only need to add to the bottom of last inputted data of planning sheet ("Sheet 2") it does not need to remove anything from ("Sheet 2")

Consistent Data: in ("Sheet 1") column ("I"), I can use this column to filter/find the Criteria ("billy") for example as this contains common names that do not change
Then ("Sheet 1") Column ("S2:S1000") has a unique number that can be VLookup or Match to ("Sheet 2") column ("K4:K1000"), this can be used to determine whether the job already exists

copy Range ("Sheet 1") is J2:W1000 or (J2:W2 to last row)
paste Range ("Sheet 2") is B:O added to bottom of last row

Hope you find this an easy task because i have been pulling my hair out trying to partially write code and macro record to find a solution to no avail
thanks
 
Upvote 0
Since this is an entirely different question, you should start a new thread.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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