![](/img/trans.png)
[英]Copy rows based on one cell value and in reference to another cell value and paste on a new sheet
[英]Copy new rows into excel sheet based on search on one cell value
我有一個我管理的 Excel 工作表,如果新行被添加到另一個我沒有管理的 Excel 工作表中,我需要添加行。
簡單地說,我可能坐在一張看起來像這樣的 excel 表上:
我需要根據文檔代碼列搜索另一個 Excel 表(參見圖 2)以查找新添加的行,如下所示:
理想情況下,我希望我的 excel 表最終看起來像這樣,如果新行包含以前不在我的 excel 表中的文檔代碼,則添加新行:
我之前看到過類似的問題,但僅針對更簡單的搜索,例如基於單元格中的固定值,例如是/否,我並沒有完全根據我的情況修改這些答案。 .
你可以試試:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.