0
votes

I have one excel sheet that I manage to which I need to add rows if new rows have been added to another excel sheet that I am not managing.

To put it simply I may be sitting with an excel sheet that looks like this:

my excel sheet

And I need to search another excel sheet (see image 2) for newly added rows based on document code column which would look like this:

The "source" excel sheet

And ideally I would like that my excel sheet would end up looking like this where new lines were added if they contained a document code not previously in my excel sheet:

My updated excel sheet

I have seen similar questions answered before but only for more simple searches like based on fixed values in the cell e.g. like yes/no and I did not quite manage to revise those answers to my situation. .

1

1 Answers

0
votes

You could try:

Sub test()

    Dim wsAll As Worksheet, wsNew As Worksheet
    Dim LastRowNew As Long, LastrowAll As Long, i As Long
    Dim rngFound As Range

    With ThisWorkbook

        'The sheet with all data
        Set wsAll = .Worksheets("All_Imports")
        'The sheet with new data
        Set wsNew = .Worksheets("New_Imports")

    End With

    With wsNew

        LastRowNew = .Cells(.Rows.Count, "A").End(xlUp).Row

        For i = 2 To LastRowNew

            Set rngFound = wsAll.Columns(1).Find(.Range("A" & i).Value, LookIn:=xlValues, LookAt:=xlWhole)

            If rngFound Is Nothing Then

                With wsAll
                    LastrowAll = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
                    .Range("A" & LastrowAll).Value = wsNew.Range("A" & i).Value
                    .Range("B" & LastrowAll).Value = wsNew.Range("B" & i).Value
                End With

            End If

        Next i

    End With

End Sub