【问题标题】:macro error when copying print area and pasting to another sheet复制打印区域并粘贴到另一张纸时出现宏错误
【发布时间】:2017-05-01 19:57:42
【问题描述】:

我已经创建了下面的代码,该代码在一定程度上可以运行,然后出现运行时错误 1004。有人可以帮忙吗?错误对话框显示“Range Class 的PasteSpecial 方法失败”并且第一行.PasteSpecial 突出显示。

Sub CopyPO()
'
' CopyPO Macro
'

'
   Dim rngPrintArea As Range
   Set rngPrintArea = ActiveSheet.Range(ActiveSheet.PageSetup.PrintArea)
With Sheets("Inventory").Range("A" & Rows.Count).End(xlUp).Offset(1)
    .PasteSpecial Paste:=xlPasteColumnWidths
    .PasteSpecial Paste:=xlPasteValues
    .PasteSpecial Paste:=xlPasteFormats
End With
End Sub

为了澄清,我正在尝试复制活动工作表的打印区域并将其粘贴到库存工作表上的下一个可用单元格中。如果范围是特定的单元格范围(即 A1:P55),则此代码有效,但如果可能,我想复制并粘贴打印区域。

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    正如您自己发现的那样,您需要复制范围,然后使用PasteSpecial。所以在你的 with-block 之前添加rngPrintArea.Copy

    稍后,如果您想将最近粘贴的单元格添加到 "Inventory" 工作表中的打印区域,那么您需要这个:

    Sub CopyPO()
    
       Dim rngPrintArea As Range
       Dim newRange As Range
    
       Set rngPrintArea = ActiveSheet.Range(ActiveSheet.PageSetup.PrintArea)
       rngPrintArea.Copy
    
       With Sheets("Inventory").Range("A" & Rows.Count).End(xlUp).Offset(1,0)
        .PasteSpecial xlPasteColumnWidths
        .PasteSpecial xlPasteValues
        .PasteSpecial xlPasteFormats
       End With
    
      Set newRange = Sheets("Inventory").Range(ActiveSheet.PageSetup.PrintArea)
    
      'If you want print area to be only the recent pasted range change the following line _
      'to commented line below;
    
      Set newRange = newRange.Resize(newRange.Rows.Count + _
                     rngPrintArea.Rows.Count, newRange.Columns.Count)
    
      'Set newRange = Sheets("Inventory").Range("A" & Rows.Count).End(xlUp).Offset(1,0). _
       'Resize(rngPrintArea.Rows.Count,rngPrintArea.Columns.Count)
    
      Sheets("Inventory").PageSetup.PrintArea = newRange.Address
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      喜欢这个

      Sub CopyPO()
      
      ' CopyPO Macro
      
         Dim rngPrintArea As Range
         Set rngPrintArea = ActiveSheet.Range(ActiveSheet.PageSetup.PrintArea)
         rngPrintArea.Copy
         With Sheets("Inventory").Range("A" & Rows.Count).End(xlUp).Offset(1,0)
              .PasteSpecial xlPasteColumnWidths
              .PasteSpecial xlPasteValues
              .PasteSpecial xlPasteFormats
         End With
      End Sub
      

      但是-我可以将复制的范围添加到库存中的打印区域...您知道我必须添加什么才能使其正常工作吗?我自己也玩过,但还没有猜到...

      【讨论】:

      • 基本上忘记复制范围了。这就是你的答案。对吗?
      • 正确。如果您也可以帮助添加打印区域,那就太好了。
      • 我真的很难弄清楚你的评论和答案的最后一段是什么意思。我不认为你想要一些硬的东西。只有你能表达自己,你才会得到帮助。
      • @Masoud - 我的意思是我想在粘贴后将复制的单元格添加到打印区域。正如你所说的不太难,我想我已经通过在 End With 之前添加 ActiveSheet.PageSetup.PrintArea = rngPrintArea 来解决它。但效果并不理想。
      • 将它们添加到Inventory 工作表的打印区域?如果是,我将发布答案。
      【解决方案3】:

      没有代码表明正在复制某些内容。如果你添加一行来复制,它看起来会起作用。

      【讨论】:

      • !我错过了!令人惊叹的你可以忽略的东西:)
      猜你喜欢
      • 1970-01-01
      • 2022-08-23
      • 1970-01-01
      • 2020-06-12
      • 2019-11-17
      • 2023-02-02
      • 1970-01-01
      • 2017-05-22
      • 1970-01-01
      相关资源
      最近更新 更多