VBA Code to Get only new values

alsharif

New Member
Joined
Mar 14, 2021
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
Hello Excel Experts,

seeking your help to privide me with vba code that can automate copying data from Column A in The Row data sheet(Row Data) to the column A in the target sheet (PTP), the point here that I want only the new values to be copied not all the values becuase the target sheet already has values that that exist in the source sheets so i want the code to update the target sheet with the new data only.

Thanks in advance brothers
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi, Great if you share the sheet having data to understand the query. Use XL2BB to share the data.

Thanks,
Saurabh
 
Upvote 0
Thank you for your quick reply bro.

below are the min sheets of the source sheet and the target sheet.

Simply I want to copy the values form column A (ID) in the sheet named (ROW Data) to column A (ID) in the target sheet named (PTP) but as you can see the PTP sheet already has existing values in column A so I do want to bring them again from the source sheet . I nead only to get the new values.

9653
1292
1237
1270

Thank you in advance.

The source sheet (ROW Data):

ID GradeStatus
7523Opending
9653AClosed
1562Bclosed
1212Epending
1292Epending
1237Apending
1270Epending
7826OClosed


The target Sheet (PTP):

ID GradeStatus
7523Opending
1562Bclosed
1212Epending
7826DClosed
 
Last edited:
Upvote 0
Hi,

Please check below code.

VBA Code:
Sub copyData()

Dim targetRow As Integer, sourceRow As Integer
Dim foundcell As Range

Application.ScreenUpdating = False

For sourceRow = 2 To Sheets("Row Data").Cells(Rows.Count, 1).End(xlUp).Row
    targetRow = Sheets("PTP").Cells(Rows.Count, 1).End(xlUp).Row + 1
    Set foundcell = Sheets("PTP").Cells.Find(what:=Sheets("Row Data").Range("A" & sourceRow), _
    after:=Sheets("PTP").Cells(1, 1), LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, _
    searchdirection:=xlNext, MatchCase:=False, searchformat:=False)
    
    If foundcell Is Nothing Then
        Sheets("Row Data").Range("A" & sourceRow & ":C" & sourceRow).Copy Sheets("PTP").Range("A" & targetRow)
    End If
Next
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Alternative:
VBA Code:
Option Explicit
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Lr1&, Lr2&, k&, cell As Range, f, arr()
Set ws1 = Worksheets("ROW Data")
Set ws2 = Worksheets("PTP")
Lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row ' current last row of "ROW Data"
Lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row ' current last row of "PTP"
ReDim arr(1 To Lr1, 1 To 3)
    For Each cell In ws1.Range("A2:A" & Lr1) ' loop through column A of "ROW Data"
        Set f = ws2.Range("A2:A" & Lr2).Find(cell) ' match each cell in column A of "PTP" with each cell in column A of "ROW Data"
        If f Is Nothing Then ' if not match, save to an array
            k = k + 1
            arr(k, 1) = cell.Value
            arr(k, 2) = cell.Offset(0, 1).Value
            arr(k, 3) = cell.Offset(0, 2).Value
        End If
    Next
ws2.Range("A" & Lr2 + 1).Resize(k, 3).Value = arr ' write to last available row of PTP
End Sub
 
Upvote 0
Solution
Hi
Try it
Excel Formula:
Sub ABC()
Dim i&, iR&
Dim ws As Worksheet
Set ws = Sheets("PTP")
With Sheets("Row data")
    iR = .Range("A" & Rows.Count).End(3).Row
    For i = 2 To iR
        If Application.WorksheetFunction.CountIf(ws.Range("A:A"), .Range("A" & i)) = 0 Then
            .Range("A" & i).Resize(, 3).Copy
            ws.Range("A" & ws.Range("A" & Rows.Count).End(3).Row + 1).PasteSpecial xlPasteValues
        End If
    Next
End With
End Sub
 
Upvote 0
Hi,

Please check below code.

VBA Code:
Sub copyData()

Dim targetRow As Integer, sourceRow As Integer
Dim foundcell As Range

Application.ScreenUpdating = False

For sourceRow = 2 To Sheets("Row Data").Cells(Rows.Count, 1).End(xlUp).Row
    targetRow = Sheets("PTP").Cells(Rows.Count, 1).End(xlUp).Row + 1
    Set foundcell = Sheets("PTP").Cells.Find(what:=Sheets("Row Data").Range("A" & sourceRow), _
    after:=Sheets("PTP").Cells(1, 1), LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, _
    searchdirection:=xlNext, MatchCase:=False, searchformat:=False)
   
    If foundcell Is Nothing Then
        Sheets("Row Data").Range("A" & sourceRow & ":C" & sourceRow).Copy Sheets("PTP").Range("A" & targetRow)
    End If
Next
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
Wooow, it works fantastically ???
I really appreciate your help and your effort bro?
Thank you very much ??
 
Upvote 0
Alternative:
VBA Code:
Option Explicit
Sub test()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim Lr1&, Lr2&, k&, cell As Range, f, arr()
Set ws1 = Worksheets("ROW Data")
Set ws2 = Worksheets("PTP")
Lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row ' current last row of "ROW Data"
Lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row ' current last row of "PTP"
ReDim arr(1 To Lr1, 1 To 3)
    For Each cell In ws1.Range("A2:A" & Lr1) ' loop through column A of "ROW Data"
        Set f = ws2.Range("A2:A" & Lr2).Find(cell) ' match each cell in column A of "PTP" with each cell in column A of "ROW Data"
        If f Is Nothing Then ' if not match, save to an array
            k = k + 1
            arr(k, 1) = cell.Value
            arr(k, 2) = cell.Offset(0, 1).Value
            arr(k, 3) = cell.Offset(0, 2).Value
        End If
    Next
ws2.Range("A" & Lr2 + 1).Resize(k, 3).Value = arr ' write to last available row of PTP
End Sub
[QUOTE="kokano90, post: 5834891, member: 487956"]
Hi
Try it [CODE=xls]
Sub ABC()
Dim i&, iR&
Dim ws As Worksheet
Set ws = Sheets("PTP")
With Sheets("Row data")
    iR = .Range("A" & Rows.Count).End(3).Row
    For i = 2 To iR
        If Application.WorksheetFunction.CountIf(ws.Range("A:A"), .Range("A" & i)) = 0 Then
            .Range("A" & i).Resize(, 3).Copy
            ws.Range("A" & ws.Range("A" & Rows.Count).End(3).Row + 1).PasteSpecial xlPasteValues
        End If
    Next
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,236
Members
448,555
Latest member
RobertJones1986

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