从一个工作簿复制到另一个工作簿 - 仅粘贴值

时间:2015-05-06 13:21:55

标签: excel-vba vba excel

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

TIA。

以下是我的代码片段:

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

3 个答案:

答案 0 :(得分:0)

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

细胞(TrIdx,4).Copy

细胞(3,2).Paste特殊粘贴:= xlPasteValues

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

答案 1 :(得分:0)

替代方法是粘贴,而不是直接设置值。

  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

答案 2 :(得分:0)

您可以将单元格的值(粘贴范围)设置为另一个单元格的值(您的复制范围),而不是按照剪贴板执行此操作:

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
相关问题