简体   繁体   English

在每个工作表匹配的单元格上复制一个单元格并将其粘贴到另一个VBA

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

相关问题 从另一张纸上的单元格值复制同一张纸中范围的一部分的粘贴范围 - Copy Range From One Sheet Paste Part of Range In Same Sheet Based On Cell Value On Another Sheet Excel VBA 从一个工作表中复制范围并将其一次一个单元格地粘贴到另一张工作表中 - Excel VBA copy range from one sheet and paste it one cell at a time in another sheet 根据单元格colorindex从一个工作表的粘贴范围复制范围到另一工作表的另一工作表 - Copy Range From One Sheet Paste Part of Range In another Sheet Based On Cell colorindex 根据单元格值将数据从一张表复制并粘贴到另一张表 - Copy and paste data from one sheet to another sheet(s) based on cell value MAC VBA 试图从每个工作表中剪切一个单元格并粘贴到下一个空单元格中的另一张表中 - MAC VBA Trying to cut one cell from each worksheet and paste in another sheet in next empty cell VBA 根据日期将工作表 1 中的单元格值复制/粘贴到工作表 2 的宏 - VBA Macro to copy/paste cell value from sheet 1 to 2 based on date VBA-将多个单元中的一个单元格复制/粘贴到主表 - VBA - copy / paste one cell from multiple workseets to master sheet 从范围循环中的每个单元格复制数据并将其粘贴到另一张纸上 - copy data from each cell in range loop and paste it on another sheet Excel VBA复制匹配条件单元格并根据不同工作表中的条件粘贴到特定单元格中 - Excel VBA to copy matching criteria cell and paste in a specific cell based on criteria in a different sheet 将单元格值(非公式)从一张纸复制并粘贴到另一张纸 - Copy and paste cell values (not formulae) from one sheet to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM