Excel Macro - Copy unique rows from one worksheet and append to another

  • Thread starter Thread starter Legacy 322463
  • Start date Start date
  • Tags Tags
    macro
L

Legacy 322463

Guest
I have a workbook with a worksheet named "Master" which holds a list of projects extracted from another system in CSV format.


I so far have a macro that imports the CSV to a new worksheet "Temp".


I then need to check for new records based on the unique reference in Column A of "Temp" and "Master". If there is a new unique row in "Temp" then it needs to be copied and appended to the end of the current list.


From searching around I have found and amended below, but this only copies the contents from Column A and doesn't append to the list in Master but replaces the rows. (I don't want the existing rows to be impacted):


Sub CopyUnique()
Dim Sh1 As Worksheet
Dim Rng As Range
Dim Sh2 As Worksheet
Set Sh1 = Worksheets("Temp")
Set Rng = Sh1.Range("A1:A" & Sh1.Range("A65536").End(xlUp).Row)
Set Sh2 = Worksheets("Master")
Rng.Cells(1, 1).Copy Sh2.Cells(1, 1)
Rng.AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sh2.Range("A1"), Unique:=True
End Sub


Any help would be greatly apprectiated!
 
If a row is found on Temp and on Master, you want to copy the row from Temp to Master, replacing the matching row in Master. Is this correct?
 
Upvote 0

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
That's correct, but it must replace it on the correct row to maintain the manually update columns.

Thanks
 
Upvote 0
Try:
Code:
Sub CopyRow()
    Application.ScreenUpdating = False
    Dim LastRow1 As Long
    LastRow1 = Sheets("Temp").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim LastRow2 As Long
    LastRow2 = Sheets("Master").Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim x As Long
    Dim rng As Range
    Dim foundVal1 As Range
    Dim foundVal2 As Range
    For Each rng In Sheets("Temp").Range("A2:A" & LastRow1)
        With Sheets("Master").Range("A:A")
            Set foundVal1 = .Find(rng, LookIn:=xlValues, lookat:=xlWhole)
            If foundVal1 Is Nothing Then
                rng.EntireRow.Copy Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
            ElseIf Not foundVal1 Is Nothing Then
                rng.EntireRow.Copy Sheets("Master").Cells(foundVal1.Row, "A")
            End If
        End With
    Next rng
    For x = LastRow2 To 2 Step -1
        With Sheets("Temp").Range("A:A")
            Set foundVal2 = .Find(Sheets("Master").Cells(x, 1), LookIn:=xlValues, lookat:=xlWhole)
            If foundVal2 Is Nothing Then
                Sheets("Master").Rows(x).EntireRow.Delete
            End If
        End With
    Next x
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
It copies the row and pastes the entire row so overwrites the custom columns with blank cells, is it possible to define the number of columns that it copies instead of the whole row?

Thanks
 
Upvote 0
Which columns would you like to copy?
 
Upvote 0
Replace this line:
Code:
 rng.EntireRow.Copy Sheets("Master").Cells(foundVal1.Row, "A")
with this line:
Code:
Sheets("Temp").Range("A" & rng.Row & ":R" & rng.Row).Copy Sheets("Master").Cells(foundVal1.Row, "A")
 
Upvote 0

Forum statistics

Threads
1,223,712
Messages
6,174,031
Members
452,542
Latest member
Bricklin

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