简体   繁体   中英

Copy Rows Into New Sheet If Value Matches In Same Column In Sheet 1 & Sheet 2

I need all rows returned into a new Sheet if there are matching values in Column G in two different sheets (Q1 DATA, Q2 DATA).

这是第一张纸(Q1数据)

这是第二张纸(Q2数据)

I placed a VLOOKUP formula =VLOOKUP('Q2 DATA'!D:D,'Q1 DATA'!D:D,2) into the 3rd sheet where I want the rows returned to, but I keep getting a #REF! error.

I'm new to Excel so I'm sure my VLOOKUP is broken, but I can't seem to figure it out. Any help would be greatly appreciated!

Assuming your data in Sheet Q1 is structured something as shown in the image below:

在此处输入图片说明

and Sheet Q2 is as:

在此处输入图片说明

Now, each row value of Column D in Sheet Q2 is to be matched with Column D of Sheet Q1 . If match found, copy range E:I from Sheet Q1 to Sheet Q2 .

Try this code:

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

This will give output in Sheet Q2 as:

在此处输入图片说明

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