【问题标题】:Why does the VBA Macro code fail when Copying and Pasting from range into new range?为什么从范围复制和粘贴到新范围时 VBA 宏代码会失败?
【发布时间】: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


【解决方案1】:

尽可能避免使用Select(以及SelectionActivate):

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
        'Copy range on Daily sheet
        Worksheets("Daily Itemized").Range("G5:S11").Copy
        'Paste it on the summary sheet commencing one cell
        ' to the right of the location of the date
        FoundCell.Offset(0, 1).PasteSpecial xlPasteValues
        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

另外,因为您只想复制值,您可以通过绕过剪贴板来改进您的代码(在您的代码执行 Copy 和当它执行Paste) 并且只是将目标区域中的Values 设置为源区域中的Values

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
        'Copy values from Daily sheet to Summary sheet, commencing
        ' one cell to the right of the location of the date
        FoundCell.Offset(0, 1).Resize(7, 13).Value = _
                    Worksheets("Daily Itemized").Range("G5:S11").Value
        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

【讨论】:

  • 虽然这可能是更好的编码实践,但我相信在这种情况下它会使最终用户无法维护生成的代码。
  • @mikemorris - 我很好奇 - 哪一点会更难维护?
  • 如果用户习惯于在他们的实践中使用 .Select(可能是依赖于宏记录器),要求他们改变这种方法将大大改变他们的工作流程/编码体验,恕我直言
  • @mikemorris - Select 的问题在于代码将被 OP 以外的人使用(根据正在执行的任务判断)。如果代码依赖于作为“活动”的特定工作表,那么其他人可能会做会破坏代码的事情。他们感到无聊,他们在窗口之间滑动,他们不小心点击了当前工作表中的某些内容。他们做各种各样的事情。所以,是的,OP 有一个学习练习,但这是他/她需要在某个阶段学习的练习,否则他们将花费所有时间调试用户问题。
  • @YowE3K 现场。很好的建议。
【解决方案2】:

感谢您的帖子。您不能从非活动工作表中选择单元格。这就是为什么它只在您使用 PASTE TO 工作表时才有效。

要解决此问题,请考虑对您的代码进行以下小改动:

    ...
If Not FoundCell Is Nothing Then
    ws.Select
    ...

这应该确保在执行其余代码之前激活 PASTE TO 工作表。

【讨论】:

  • 谢谢!我也会试试这个,虽然我已经为上面的第一个答案更新了我的代码。
猜你喜欢
  • 2018-09-27
  • 1970-01-01
  • 1970-01-01
  • 2016-08-30
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多