【问题标题】:Copy Rows to different sheets, IF cell value matches to another sheet将行复制到不同的工作表,如果单元格值匹配到另一个工作表
【发布时间】:2020-02-15 13:55:25
【问题描述】:

我有 2 个工作表。 sheet1 是 col A 上的每月值。 Sheet 2 是 col A 上的每日值,我希望 Excel 在工作表 2(每日)中查找相同的值,然后一旦找到该确切值,复制匹配的行表单表 1 (每月)并将其粘贴到表 2(每日)中。 任何想法如何编写将自动复制和粘贴值过程的 VBA 代码? (参见屏幕截图) [1]:https://i.stack.imgur.com/G5LqW.png [在右侧,数据是每月(每个月的最后一天),我需要匹配两张表的 col A,并在确切的日期(该月的最后一天)将数据带到另一张表][1]

【问题讨论】:

  • 欢迎来到 SO ;) 请在 how to ask a good question 上阅读这篇文章。这将包括正确描述您要实现的目标、您的代码(或相关的 sn-ps)以及显示您迄今为止尝试过的内容和可能的错误消息的努力。
  • @iLuvLogix ,谢谢,我添加了一个屏幕截图,它可能会有所帮助

标签: excel vba


【解决方案1】:

未经测试

Sub Copy18()
Dim wb As Workbook
Dim wsD2 As worksheet, wsM2 As Worksheets
Dim LastRow As long, LastCol As Long, i as long
Dim Cell As Range, Rng As Range, SearchR As Range, CopyRng As Range, PasteRng As Range

Set wb = ThisWorkbook
Set wsD2 = wb.Sheets("Daily-2")
Set wsM2 = wb.Sheets("Monhly-2")

LastCol = wsM2.Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = wsM2.Cells(Rows.Count, 1).End(xlUp).Row

Set Rng = wsD2.Range(wsD2.Cells(1,1), wsD2.Cells(LastRow, LastCol))

For Each Cell in Rng
    Set SearchR = wsM2.Range("A:A").Find(Cell.Value, LookAt:=xlWhole)
    If Not SearchR Is Nothing Then
        i = LastCol = wsM2.Cells(SearchR.Row, Columns.Count).End(xlToLeft).Column
        Set CopyRng = wsM2.Range(wsM2.Cells(SearchR.Row, 1), wsM2.Cells(SearchR.Row, i))
        Set PasteRng = wsD2.Range(wsD2.Cells(LastRow + 1, 1), wsD2.Cells(LastRow + 1, i))
        PasteRng.Value = CopyRng.Value
        LastRow = LastRow + 1
    End If
Next Cell

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多