如果值在Sheet 1和Sheet 2中的相同列中匹配,则将行复制到新工作表中
如果在两个不同的工作表(Q1 DATA,Q2 DATA)的列G中有匹配的值,我需要将所有行返回到新工作表中。
我把VLOOKUP
公式=VLOOKUP('Q2 DATA'!D:D,'Q1 DATA'!D:D,2)
放到第三张表格中,我想要返回的行,但我一直得到一个#REF! 错误。
我是新来的Excel,所以我确定我的VLOOKUP
已经坏了,但我似乎无法弄清楚。 任何帮助将不胜感激!
假设您在Sheet Q1
的数据结构如下图所示:
和Sheet Q2
是:
现在, Sheet Q2
Column D
每一行值都要与Sheet Q1
Column D
相匹配。 如果find匹配项,请将范围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
输出: