【问题标题】:Find matching rows across two sheets在两张纸上查找匹配的行
【发布时间】:2020-02-05 13:52:43
【问题描述】:

我有一张 sheet1 (sh1),其中我在 (A2) 中有一个国家名称,在 (B2) 中有一个方向。

我想在 sheet2 (sh2) 上找到一行,其中 A 列包含相同的城市名称,B 列包含相同的地区,并将整行复制到 sh1 上匹配的行旁边。然后我想遍历 sh1 上的所有行并在 sh2 上找到匹配的行并以相同的方式复制它。

我似乎在复制数据,但 sh2 上的匹配行包含我想复制到 sh1 的其他信息。

举例说明:

On Sheet1:

Column A               Column B
(header)               (header)
San Diego              South
New York               North
Chicago                East

On Sheet2:

Column A              Column B
(header)              (header)
Chicago               East              
San Diego             South
New York              North

循环将首先检查圣地亚哥,然后是纽约,然后是芝加哥,依此类推,直到列结束。

这是我的代码:

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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    你已经很接近了,试试这个更正的代码(更正在 cmets 中):

    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,只需一次。

    【讨论】:

    • 修改Destination:=sh2.Range("C" & x)sh2.Rows(x),他要求整行(附加数据):)
    • 两种方法都有效。您可以复制整行并将其粘贴到第一列,或者仅复制从第三列开始的不同数据子集并将其粘贴到第三列。但是您不能将整行粘贴到 C 列,因为它不适合。
    • 如果sh1.Range("A" & x).EntireRow.Copy sh2.Range("C" & x) 由于大小不同,该行无法正常工作(我假设他在一张纸中的数据比第二张更多)。
    • 我刚刚注意到我忘记删除代码中的.EntireRow,它不应该存在。您提到的那一行不起作用,因为它正在复制具有Rows.Count 行数的范围,并试图将其粘贴到具有Rows.Count - 2 行数的范围。复制的范围不适合目标范围。
    • 我知道'为什么',我只是想将代码调整为.EntireRow,仅此而已:) 但现在你发现它不需要了,编辑了,现在一切都很好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    相关资源
    最近更新 更多