简体   繁体   English

将行复制到不同的工作表,如果单元格值与另一个工作表匹配

[英]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.

相关问题 根据单元格值将多张纸中的行复制到一张纸中 - Copy rows from multiple sheets into one sheet based on a cell value 如果C的单元格值与工作表名称匹配,如何将行复制到另一工作表 - How to copy row to another sheet if the cell value of C matches the sheetname 根据单元格中的值将行从一张表复制到另一张表中 - Copy rows from one sheet into another depending on value in cell 根据单元格值查找所有行,然后复制到其他工作表。 - Find all rows based on a cell value and copy to different sheet. 如果其他列的单元格中的值与其他工作表中的值匹配,则链接不同工作表中一列的两个单元格 - Link two cells of a column in different sheets if a value in a cell of other column matches in other sheet 如果值在工作表1和工作表2中的同一列中匹配,则将行复制到新工作表中 - Copy Rows Into New Sheet If Value Matches In Same Column In Sheet 1 & Sheet 2 如果单元格与另一个单元格匹配,则复制值 - If Cell Matches another Cell, Copy Value 返回与搜索匹配的单元格的地址并将接下来 X 行中的所有内容复制到另一个工作表 - Return the address of a cell that matches a search and copy everything in the next X rows to another sheet 如何比较2张表中的行,然后将不同的行复制到另一张表vba - how to compare rows from 2 sheets and then copy the different rows to another sheet vba 基于一个单元格值复制行并引用另一个单元格值并粘贴到新工作表上 - Copy rows based on one cell value and in reference to another cell value and paste on a new sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM