匹配

时间:2016-09-24 06:37:40

标签: excel-vba vba excel

  • 第一张纸包含日期。
  • 第二张包含第3行的相同日期。
  • 找到匹配项后,将偏移2行并复制范围 从第一张到此处的数据。

我的非工作代码:

Sub test()
    Dim ws As Worksheet
    Dim rng As Range
    Dim targetDate As Range

    'change sheet1 to suit
    Set ws = ThisWorkbook.Worksheets("My Sheet")

    'change address of your cell with target date
    Set targetDate = ThisWorkbook.Worksheets("Instructions").Range("A4")

    'tries to find target date in first row
    Set rng = ws.Range("3:3").Find(What:=targetDate, LookAt:=xlWhole, MatchCase:=False)

    If rng Is Nothing Then
        'if nothing found
        MsgBox ("")
    Else
        'if date is found - write something below the date
        Sheets("Scribble Pad").Range("B2:D11").Copy
        ws.Activate
        rng.Offset(2).Value = ActiveSheet.Paste
        Application.CutCopyMode = False
    End If
End Sub

0 个答案:

没有答案