VBA Excel:粘贴到工作簿之间的匹配行

时间:2013-03-15 17:38:05

标签: excel vba

我正在尝试开发代码来执行以下操作:

1)Copy cells K4: M4 in Workbook 1, Sheet 1 <- I can do this step;

2)Find a cell in Workbook2, Sheet1, column C that matches cell B4 in
Workbook1, Sheet1;

3)Paste the copied values in columns P:R of the matching row in
Workbook 2, Sheet 1 as determined in Step 2.

我提前道歉,因为我无法将自己的工作推进到第1步之后。正如我所说的那样,我已经完全陌生了,并且已经在网上搜索答案/了解到这一点,而没有提出解决方案

1 个答案:

答案 0 :(得分:1)

我对此进行了测试并且有效。这有助于您入门吗?

Sub CopyToMatchedRow()
    Dim copyRng As Range, matchVal As Variant, matchRng As Range, matchRow As Integer

    Set copyRng = Worksheets("Sheet1").Range("K4:M4")
    Set matchRng = Worksheets("Sheet2").Range("C:C")
    matchVal = Worksheets("Sheet1").Range("B4")
    matchRow = matchRng.Find(What:=matchVal, After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

    copyRng.Copy Destination:=Worksheets("Sheet2").Range("P" & matchRow & ":R" & matchRow)
End Sub