[英]Copy Rows Into New Sheet If Value Matches In Same Column In Sheet 1 & Sheet 2
假設工作Sheet Q1
的數據結構如下圖所示:
Sheet Q2
如下:
現在,工作Sheet Q2
Column D
每一行值都將與工作Sheet Q1
Column D
匹配。 如果找到匹配項,則將范圍E:I
從Sheet Q1
復制到Sheet Q2
。
試試這個代碼:
Sub Demo()
Dim data1WS As Worksheet, outputWS As Worksheet
Dim lastRow As Long
Dim myRange As Range, rFound As Range
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Set dataWS = ThisWorkbook.Sheets("Sheet Q1")
Set outputWS = ThisWorkbook.Sheets("Sheet Q2")
lastRow = dataWS.Cells(Rows.Count, "D").End(xlUp).row
Set myRange = Range(dataWS.Cells(2, 4), dataWS.Cells(lastRow, 4))
For Each cel In myRange
Set rFound = outputWS.Columns(4).Find(What:=cel.Value, LookIn:=xlValues, LookAt:=xlWhole)
If Not rFound Is Nothing Then
Range(outputWS.Cells(cel.row, 5), outputWS.Cells(cel.row, 9)).Value = Range(dataWS.Cells(cel.row, 5), dataWS.Cells(cel.row, 9)).Value
End If
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
這將在工作Sheet Q2
給出如下輸出:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.