![](/img/trans.png)
[英]How to copy and paste a cell value to another worksheet in the same workbook
[英]How to copy and paste a range to another worksheet by matching a cell value
我需要通过将一个单元格中的值与另一个表中具有该值的行进行匹配,来将一个工作表的范围复制到另一个工作表。 例如,我需要通过将单元格A2(sheet1)中的值匹配到sheet2上具有匹配单元格值的行来复制sheet1上的范围E2:H2。 例如,在我的图片中,sheet1的ID为10,其中包含一些数据。 E2:H2列应复制到工作表2上ID为10的行。
Sheet1示例(数据源)
Sheet2示例(数据目标)
这将是在此子代码中调用的更大代码的一部分,由于数据的大小,我举了一个示例,而不是使用实际的电子表格。
这是一些修改后的代码,它与我目前对这段代码(更改的范围和工作表名称)非常相似。 我知道我要粘贴的内容无法满足我的需求,但是找不到与我所需的内容相似的内容。 谢谢!
sub copydata()
'Range of cells to be copied to other sheet
Const strRANGE as String = "E2:H2"
set shSource=Sheets("Sheet1")
set shTarget=Sheets("Sheet2")
'Copy range from source sheet (sheet1)
shSource.Range(strRANGE).copy
'Paste range from Source sheet(sheet1) to Sheet 2 by matching ID value (cell A2)
shTarget.Range("A65000").End(xlUp).Offset(1).PasteSpecial xlValues
end sub
这样的事情会起作用,您可以利用Range.Find
函数在目标表中找到ID。
Sub copydata()
Set shSource = Sheets("Sheet1")
Set shTarget = Sheets("Sheet2")
Dim fnd As Range
Set fnd = shTarget.Range("A1:A1000").Find(shSource.Range("A2").Value)
If Not fnd Is Nothing Then
shSource.Range("E2:H2").Copy
shTarget.Cells(fnd.row, 5).PasteSpecial xlPasteValues
End If
End Sub
目前,它仅适用于第二行,将其放在多行循环中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.