简体   繁体   中英

Copy new rows into excel sheet based on search on one cell value

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:

我的excel表

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:

“源”excel表

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:

我更新的 Excel 表

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM