2016-12-06 101 views
0

我在Excel中有一個宏,但是當我嘗試使用Selection.PasteSpecial進行粘貼時,它向我顯示錯誤1004。vb excel error 1004 Selection.PasteSpecial

如果我嘗試使用Office 2010中的相同宏,則此錯誤與Office 365無關。

調試器示出了在這一行一個錯誤:

Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
     :=False, Transpose:=False 

模塊的完整代碼是:

Sub MoveData_Activations() 

MoveData_Activations Macro 

    Dim dayCount As Integer 
    Dim startCell As String 
    Dim curCellRef As String 

    dayCount = 13 
    startCell = "B3" 

    If MsgBox("Are you sure you want to rollover the data for a new date?", vbYesNo, "Confirm rollover") = vbYes Then 
     ActiveSheet.Protect UserInterfaceOnly:=True 
     Range(startCell).Select 
     ActiveCell.Cells(-1, 6).Copy 
     ActiveCell.Cells(-1, 1).Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
      :=False, Transpose:=False 

     curCellRef = Range(startCell).Address 
     For i = 1 To dayCount 
      CopyData curCellRef, False, 6, 24, 2 
      curCellRef = ActiveCell.Cells(1, 6).Address 
     Next i 
     CopyData curCellRef, True, 6, 24, 2 
    End If 
End Sub 
+0

不知道,但閱讀[這](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)。 – Kyle

回答

0

它總是耗時在剪貼板拷貝內容,這可能是因爲要麼被另一個程序使用,因此錯誤或內存不知何故被搞亂。 對於短,改變這些行:
來源:

Range(startCell).Select 
     ActiveCell.Cells(-1, 6).Copy 
     ActiveCell.Cells(-1, 1).Select 
     Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
      :=False, Transpose:=False 

要:

Range(startCell).Offset(-1, 6).Copy destination:=Range(startCell).Offset(-1, 1) 

Hint: Avoid selection