Excel 在两张图纸中查找匹配行
我有一张sheet1 sh1,其中国家名称在A2,方向在B2 我想在sheet2 sh2上找到一行,其中columnA包含相同的城市名称,columnB包含相同的地区,并将整行复制到sh1上匹配的行旁边。然后,我想遍历sh1上的所有行,找到sh2上匹配的行,并以相同的方式复制它 看起来我在复制数据,但sh2上匹配的行包含我想复制到sh1的其他信息 举例说明: 循环将首先检查圣地亚哥,然后是纽约,然后是芝加哥等等,直到专栏结束 这是我的密码:Excel 在两张图纸中查找匹配行,excel,vba,Excel,Vba,我有一张sheet1 sh1,其中国家名称在A2,方向在B2 我想在sheet2 sh2上找到一行,其中columnA包含相同的城市名称,columnB包含相同的地区,并将整行复制到sh1上匹配的行旁边。然后,我想遍历sh1上的所有行,找到sh2上匹配的行,并以相同的方式复制它 看起来我在复制数据,但sh2上匹配的行包含我想复制到sh1的其他信息 举例说明: 循环将首先检查圣地亚哥,然后是纽约,然后是芝加哥等等,直到专栏结束 这是我的密码: Sub Matchcountry() Dim
Sub Matchcountry()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim r As Range
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
r = lastrow = sh1.Range("A" & Rows.Count) + 2.End(xlUp).Row
For x = 1 To r
If sh1.Range("A" & x) = sh2.Range("A" & x) And sh1.Range("B" & x) = sh1.Range("A" & x) & sh2.Range("B" & x) Then
sh1.Range("A" & x).EntireRow.Copy Destination:=sh2.Range("C" & x)
x = x + 1
Next x
End Sub
您已经非常接近了,请尝试以下更正的代码更正在注释中:
Sub Matchcountry()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim r As Long, r2 As Long 'we just need the row number, not the Range object
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
r = sh1.Range("A" & Rows.Count).End(xlUp).Row 'All the necessary parts were there, just the syntax was wrong
r2 = sh2.Range("A" & Rows.Count).End(xlUp).Row
Dim x As Long, y As Long 'It's good practice to declare all your variables
For x = 1 To r
For y = 1 To r2
If sh1.Cells(x, 1).Value2 = sh2.Cells(y, 1).Value2 And sh1.Cells(x, 2).Value2 = sh2.Cells(y, 2).Value2 Then 'Again, most necessary parts were already there
sh1.Range(sh1.Cells(x, 1), sh1.Cells(x, Columns.Count).End(xlToLeft)).Copy Destination:=sh2.Range("C" & y) 'We don't need the entire row, in fact we won't be able to copy it to the desired renage since it's too big
Exit For 'will stop the second loop once it's found a match
End If
Next y
'x = x + 1 Common mistake. Next x already iterates x, by doing it this way we skip every second step
Next x
End Sub
最大的变化是第二个For循环。我们需要第二个循环,因为您希望对sh1的每一行循环一次sh2,nut。修改Destination:=sh2.range c&x到sh2.Rowsx,他要求整行附加数据:两种方法都有效。您可以复制整行并将其粘贴到第一列,也可以只复制从第三列开始的数据子集并将其粘贴到第三列。但是您不能将整行粘贴到C列,因为它不适合。如果sh1.RangeA&x.EntireRow.Copy sh2.RangeC&x这一行不起作用,因为大小不同,我假设一张工作表中的数据比第二张工作表中的数据多。我刚刚注意到我忘记在代码中删除.EntireRow,这是不应该存在的。您提到的那行行不通,因为它正在复制一个包含行数的区域。请计算行数,并尝试将其粘贴到包含行数的区域。请计算-2行数。复制的范围不适合目标范围。我知道“为什么”,我只是想将代码调整为.EntireRow,仅此而已:但现在您发现它不需要,经过编辑,现在一切正常:
Sub Matchcountry()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim r As Long, r2 As Long 'we just need the row number, not the Range object
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
r = sh1.Range("A" & Rows.Count).End(xlUp).Row 'All the necessary parts were there, just the syntax was wrong
r2 = sh2.Range("A" & Rows.Count).End(xlUp).Row
Dim x As Long, y As Long 'It's good practice to declare all your variables
For x = 1 To r
For y = 1 To r2
If sh1.Cells(x, 1).Value2 = sh2.Cells(y, 1).Value2 And sh1.Cells(x, 2).Value2 = sh2.Cells(y, 2).Value2 Then 'Again, most necessary parts were already there
sh1.Range(sh1.Cells(x, 1), sh1.Cells(x, Columns.Count).End(xlToLeft)).Copy Destination:=sh2.Range("C" & y) 'We don't need the entire row, in fact we won't be able to copy it to the desired renage since it's too big
Exit For 'will stop the second loop once it's found a match
End If
Next y
'x = x + 1 Common mistake. Next x already iterates x, by doing it this way we skip every second step
Next x
End Sub