【发布时间】:2017-05-14 07:27:34
【问题描述】:
在识别我要粘贴到的工作表上的单元格值到与 COPY FROM 工作表上的单元格匹配的日期之后,我试图将一系列数据从一个工作表复制到另一个工作表。当我从 PASTE TO 工作表(“每日摘要记录”)运行宏时,该宏有效,但如果我从另一个工作表运行它,则该宏不起作用。我希望能够从工作簿中的任何工作表运行它,尤其是从 PASTE FROM 工作表。
下面是我的代码:
'Daily Itemized')
Sub ArchiveWeek()
Set thisMon = Worksheets("Daily Itemized").Range("F5") 'Assigns variable thisMon as the date value in Daily Itemized Tab, F5 cell
Dim ws As Excel.Worksheet
Dim FoundCell As Excel.Range
Set ws = Worksheets("Daily Summary Record")
Set FoundCell = ws.Range("D:D").Find(what:=thisMon, lookat:=xlWhole)
If Not FoundCell Is Nothing Then
FoundCell.Offset(0, 1).Select 'PROBLEM. Supposed to: Selects the cell to the adjacent right of the cell in column D with the same date as the Itemized F5 cell
Worksheets("Daily Itemized").Range("G5:S11").Copy 'Works to copy range on Daily sheet
FoundCell.Offset(0, 1).Select 'reselects the cell to right of FoundCell
Selection.PasteSpecial xlPasteValues 'works!
MsgBox ("Your week time values have been pasted!")
Else
MsgBox ("The Date of " & thisMon & " was not found in the Daily Summary Record, Column D. Recheck values.")
End If
End Sub
两个工作表的图片见附件:
“每日明细”
'每日摘要记录'
【问题讨论】:
-
我应该补充一点,我是一个非常新手的 VBA 编码器。我只是刚刚扩展过去使用宏记录器编写原始代码,所以小步骤是合适的......而且我完全是自学的。
-
上面的选项 1 有效!所以谢谢你!
-
当我尝试选项 #2 时,这行代码出现调试错误:
-
抱歉,它不允许我编辑我的最后一条评论。这行代码:FoundCell.Offset(0, 1).Resize(7, 13).Values = _ Worksheets("Daily Itemized").Range("G5:S11").Values
标签: excel copy-paste vba