如果值在第1页和第1页中的相同列中匹配,则将行复制到新工作表中第2页

时间:2016-07-01 17:01:07

标签: excel excel-vba excel-formula vba

如果G列中的两个不同表(Q1 DATA,Q2 DATA)中存在匹配值,则需要将所有行返回到新工作表中。

This is the 1st Sheet (Q1 DATA)

This is the 2nd Sheet (Q2 DATA)

我将VLOOKUP公式=VLOOKUP('Q2 DATA'!D:D,'Q1 DATA'!D:D,2)放入第3页,我想要返回的行,但我一直得到#REF!错误。

我是Excel的新手,所以我确信我的VLOOKUP已被破坏,但我似乎无法弄明白。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

假设Sheet Q1中的数据结构如下图所示:

enter image description here

Sheet Q2如下:

enter image description here

现在,Column DSheet Q2的每个行值都与Column D的{​​{1}}匹配。如果找到匹配项,请将范围Sheet Q1E: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 中的输出显示为:

enter image description here