[英]Copy Rows to different sheets, IF cell value matches to another sheet
I have 2 worksheets.我有 2 个工作表。 sheet1 is monthly Value on col A. Sheet 2 is daily value on col A, I would Like Excel to look for the same value in worksheet 2 (Daily), then once it finds that exact value, to copy the matched rows form sheet 1 (monthly) and paste it in sheet 2 (Daily). sheet1 是 col A 上的每月值。 Sheet 2 是 col A 上的每日值,我希望 Excel 在工作表 2(每日)中查找相同的值,然后一旦找到该确切值,就复制匹配的行表单表 1 (每月)并将其粘贴到工作表 2(每日)中。 Any ideas how to write a VBA code that will automate this copy and paste values process?任何想法如何编写将自动执行此复制和粘贴值过程的 VBA 代码? (see the screenshot ) [1]: https://i.stack.imgur.com/G5LqW.png [on the right handside, data is per month (last day of each month), i need to match col Aof both sheets, and bring the data to other sheet on exact day (last day of that month)][1] (见截图)[1]: https : //i.stack.imgur.com/G5LqW.png [在右侧,数据是每个月(每个月的最后一天),我需要匹配两张表的A列,并在确切的日期(该月的最后一天)将数据带到其他工作表][1]
Untested未经测试
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.