2016-11-11 316 views
1

我有一点点麻烦,我想一列从另一个工作簿复制到一个新的工作簿和调换,但我有这样的错误:Error 1004: PasteSpecial method of range class failed错误1004:Range类的PasteSpecial方法失败

Private Sub CommandButton1_Click() 

    ActiveSheet.Range("C2:C30").Copy 
    Workbooks.Open Filename:="E:\PENDIDIKAN_BJN\SUM.xlsx" 
    eColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column 
    If eColumn >= 1 Then eColumn = eColumn + 1 
    ActiveSheet.Cells(1, eColumn).Select 
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, Skipblanks:=False, Transpose:=True 
    ActiveWorkbook.Save 
    ActiveWorkbook.Close 
    Application.CutCopyMode = False 

End Sub 

回答

1

原因问题

的你是通过转他们试图在C2单元格复制到C30,所以29个细胞(30-2 + 1)到其他工作簿。换句话说,将这些单元格粘贴为29列的单行。

在您的其他工作簿中,您使用If eColumn >= 1 Then eColumn = eColumn + 1“动态”选择了列,因此您不一定会选择需要执行粘贴的29列。

最后的结果是错误消息Error 1004: PasteSpecial method of range class failed

解决方案

解决的办法之一是直接选择粘贴数据列的权数,这样做是这样的:

Private Sub CommandButton1_Click() 

    ActiveSheet.Range("C2:C30").Copy 
    ' Store the number of rows selected in the eColumn associated before opening the other file 
    ' Since this is a transposition the number of rows is the same as the target number of columns 
    eColumn = Selection.Rows.Count 

    Workbooks.Open Filename:="E:\PENDIDIKAN_BJN\SUM.xlsx" 
    ActiveSheet.Cells(1, eColumn).Select 
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, Skipblanks:=False, Transpose:=True 
    ActiveWorkbook.Save 
    ActiveWorkbook.Close 
    Application.CutCopyMode = False 

End Sub 
相关问题