[英]Copy a cell on one sheet and paste it on another VBA based on cell from each sheet matching
I am hoping someone can help me out here. 我希望有人可以在这里帮助我。 I have the below code returning an error message when I run it.
我有以下代码,在运行它时会返回错误消息。 I have a report that I import every hour into Sheet2.
我有一个报告,每小时都会导入Sheet2。 I need to take the value in cell D16 and copy it.
我需要在单元格D16中获取值并将其复制。 Then I need to match Sheet2!A2 to the cell in Row 1 on Sheet3 and paste the data under the corresponding column.
然后,我需要将Sheet2!A2与Sheet3的第1行中的单元格匹配,并将数据粘贴到相应列下。
I would appreciate any input or suggestions to resolve this. 对于解决此问题的任何意见或建议,我将不胜感激。
Thanks in advance! 提前致谢!
Sub CopyPaste()
Dim ws1 As Worksheet, ws2 As Worksheet, rng As Range, frng As Range
Set ws1 = Worksheets("Sheet2")
Set ws2 = Worksheets("Sheet3")
Set rng = ws1.Range("D16")
Set frng = ws2.Rows(1).Find(What:=Range("Sheet2!A2"), After:=Range("Sheet3!A1"), LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
rng.Copy
frng.Offset(1, 0).PasteSpecial (xlPasteValues)
Application.CutCopyMode = 0
End Sub 结束子
I would do this one: 我会这样做:
Sub CopyPaste()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim res
Set ws1 = Worksheets("Sheet2")
Set ws2 = Worksheets("Sheet3")
res = Application.Match("*" & ws1.Range("A2") & "*", ws2.Range("1:1"), 0)
If IsError(res) Then
MsgBox "Nothing found"
Exit Sub
End If
ws2.Cells(2, res).Value = ws1.Range("D16").Value
End Sub
for exact match use res = Application.Match(ws1.Range("A2"), ws2.Range("1:1"), 0)
要进行完全匹配,请使用
res = Application.Match(ws1.Range("A2"), ws2.Range("1:1"), 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.