在两张纸之间复制单元格

时间:2014-10-30 18:35:16

标签: excel vba excel-vba copy

我尝试做的只是根据单元格的内容将信息从一张纸复制到另一张。基本上,如果A列中的数字,表1与A列中的数字相匹配,则表2我希望它将表2中的整行复制到表1,两行单元格分隔该行在哪里匹配。

任何帮助将不胜感激!如果您需要进一步澄清,请与我们联系。

For i = 1 to EndofColumnInSheet1
       if (There is a match with xNumber in sheet 2)
           {
              Copy entire row in Sheet 2 beside matching row in Sheet 1 
           }

       else
            {
                Keep entire row empty.
                AKA Skip this row.
            }

1 个答案:

答案 0 :(得分:1)

这个简单的循环似乎运行得相当快。

Dim r As Long, cc As Long, cr As Long, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Sheets("Sheet10")
Set ws2 = Sheets("Sheet11")

With ws1
    For r = 1 To .Cells(Rows.Count, 1).End(xlUp).Row
        If CBool(Application.CountIf(ws2.Columns(1), .Cells(r, 1).Value)) Then
            cr = Application.Match(.Cells(r, 1).Value, ws2.Columns(1), 0)
            cc = ws2.Cells(cr, Columns.Count).End(xlToLeft).Column
            .Cells(r, 4).Resize(1, cc) = ws2.Cells(cr, 1).Resize(1, cc).Value
        Else
            'do nothing
        End If
    Next r
End With

Set ws2 = Nothing
Set ws1 = Nothing