【问题标题】:Copy from one workbook to another - paste values only从一个工作簿复制到另一个工作簿 - 仅粘贴值
【发布时间】:2015-05-06 21:43:20
【问题描述】:

我有一个小问题,我正在尝试解决从一个工作簿复制到另一个工作簿的问题。执行复制工作的两行,但我只想复制值。事实上,它复制数据但保留源工作簿中的单元格格式。您会注意到,当使用时我已经注释掉了行尾,会引发运行时错误 1004(“无法获取范围类的 PasteSpecial 属性”)。我用谷歌搜索查看了类似的问题,但找不到适合我的问题。我正在寻求解决这个问题,以便我可以继续我的项目。

TIA。

这是我的代码的 sn-p:

Private Sub CommandButton77_Click()
'Individual Induction forms - Trainee #1

Dim wbPrint As Workbook, TrIdx As Integer
Application.ScreenUpdating = False
Set wbPrint = Workbooks.Open("c:\temp\Drivers Induction Checklist.xlsx", ReadOnly:=True)
With wbPrint.Sheets("Induction Depot")
  TrIdx = 66 'Index for Trainee #1
  If ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4) <> "" Then
    'Depot Induction Sheet
    ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4).Copy Destination:=.Cells(3, 2) '.PasteSpecial(Paste:=xlPasteValues) 'Trainee Name (Top)
    ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4).Copy Destination:=.Cells(60, 3) '.PasteSpecial(Paste:=xlPasteValues) 'Trainee Name (Bottom)
    .PrintOut
  End If
        'Next
       wbPrint.Close SaveChanges:=False
End With
End Sub

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我可能会简化一些事情并在不同的行上做:

    Cells(TrIdx, 4).复制

    Cells(3, 2).PasteSpecial Paste:=xlPasteValues

    似乎对我有用,并且不会跨格式复制

    【讨论】:

      【解决方案2】:

      另一种方法不是粘贴,而是直接设置值。

        If ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4) <> "" Then
          'Depot Induction Sheet
          ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4).Copy Destination:=.Cells(3, 2) '.PasteSpecial(Paste:=xlPasteValues) 'Trainee Name (Top)
          ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4).Copy Destination:=.Cells(60, 3) '.PasteSpecial(Paste:=xlPasteValues) 'Trainee Name (Bottom)
          .PrintOut
        End If
      

      可能变成:

        If ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4) <> "" Then
          'Depot Induction Sheet
          .Cells(3, 2) = ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4)
          .Cells(60, 3) = ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4)
          .PrintOut
        End If
      

      【讨论】:

      • 这对我来说几乎已经完成了。谢谢你——正如我对 JNevill(上图)所说,我需要一个全新的视角来解决这个小僵局。非常感谢:)
      【解决方案3】:

      您可以将单元格的值(您的粘贴范围)设置为另一个单元格的值(您的复制范围),而不是点击剪贴板:

      Private Sub CommandButton77_Click()
      'Individual Induction forms - Trainee #1
      
      Dim wbPrint As Workbook, TrIdx As Integer
      Application.ScreenUpdating = False
      Set wbPrint = Workbooks.Open("c:\temp\Drivers Induction Checklist.xlsx", ReadOnly:=True)
      With wbPrint.Sheets("Induction Depot")
        TrIdx = 66 'Index for Trainee #1
        If ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4) <> "" Then
          'Depot Induction Sheet
          .Cells(3, 2).Value = ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4).Value 'Trainee Name (Top)
          .Cells(60, 3).Value = ThisWorkbook.Sheets("Stats").Cells(TrIdx, 4).Value 'Trainee Name (Bottom)
          .PrintOut
        End If          
        wbPrint.Close SaveChanges:=False
      End With
      
      Application.ScreenUpdating = True 'Don't forget to turn this back on :o
      End Sub
      

      【讨论】:

      • 感谢您的建议!这对我来说已经成功了。在我看来,最简单的方法是最好的。显然,我看不到树的木头,很高兴从另一个角度看到这一点。
      猜你喜欢
      • 2013-10-21
      • 2017-09-08
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多