循环搜索工作簿1中的数据,将偏移量单元格复制到工作簿2中

时间:2019-10-20 06:11:11

标签: excel vba

我需要一个循环,将从SOURCE(基于DESTINATION中的范围)中找到的值偏移的单元格复制到DESTINATION。

在这种情况下,我想根据DESTINATION中的值(“ H5”)在源中找到的值(“ E10”)之后,将值从SOURCE(“ K10”)复制到DESTINATION(“ G5”)。

我需要搜索DESTINATION(“ H:H”)中的所有值。

Book_source.xlsx enter image description here

Book_destination.xlsx enter image description here

我的录制代码:

 Windows("Book_destination.xlsx").Activate
    Dim rng As Variant
    rng = Range("H5").Value

    rng.Select
    Selection.Copy
    Application.WindowState = xlNormal

    Windows("Book_source.xlsx").Activate
    Cells.Find(What:=rng, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

    ActiveCell.Offset(0, 6).Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("Book_destination.xlsx").Activate
    Range("G5").Select
    ActiveSheet.Paste

1 个答案:

答案 0 :(得分:0)

我创建了此代码,并正在为我工​​作。 对于任何有兴趣的人

谢谢大家。 :) 享受,它是免费的!

很高兴与大家分享。

Sub part_of_code()

    Dim i As Integer
    i = 2

    'calling LastRow
    Call LastRecord(LastRow)

    For i = i To LastRow
        On Error Resume Next
        'Application.WindowState = xlNormal
        Range("H" & i).Select
        Selection.Copy
        Dim rng As Variant
        rng = Range("H" & i)

        Workbooks("Book2.xlsx").Worksheets("Sheet1").Activate
        Cells.Find(What:=rng, After:=ActiveCell, LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Offset(0, 6).Select
        Application.CutCopyMode = False
        Selection.Copy
        Workbooks("Book1.xls").Worksheets("Sheet2").Activate
        Range("H" & i).Offset(0, -1).Select
        ActiveSheet.Paste
    Next i

End Sub

Private Sub LastRecord(LastRow)
  With ActiveSheet
     LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  End With
End Sub