VBA代码不一致崩溃Excel(运行时错误80010108)

时间:2012-08-30 20:34:32

标签: excel-vba vba excel

我正在使用以下VBA代码自动将一张纸上的行移动到另一张纸上。大约1/3的时间,它给出“运行时错误'-2147417848(80010108)”,然后崩溃Excel。我找不到一个共同的理由。崩溃后,我可以在同一行执行相同的代码,下次可能会或可能不会正常工作。

有人能说出为什么下面这段代码应该不稳定吗?

Sub Move_to_Sheet2 ()
'
' Move_to_Sheet2 Macro
'
' Keyboard Shortcut: Ctrl+r
'
    Rows(ActiveCell.Row).Select
    Selection.Copy
    Set Rng = Nothing
    Sheets("Sheet2").Select
    Rows("4:4").Select
    Selection.Insert Shift:=xlDown
    Sheets("Sheet1").Select
    Selection.Delete Shift:=xlUp
    ActiveWorkbook.save
End Sub

2 个答案:

答案 0 :(得分:2)

您需要完全符合您的行数。见这个例子。

Sub Move_to_Sheet2()
    Dim ws As Worksheet

    '~~> Change this to the relevant sheet name
    Set ws = Sheets("Sheet1")

    With ws
        .Rows(ActiveCell.Row).Copy
        Sheets("Sheet2").Rows("4:4").Insert Shift:=xlDown
        .Rows(ActiveCell.Row).Delete
    End With
    ActiveWorkbook.Save
End Sub

答案 1 :(得分:0)

Sub Test()

' Test Macro

    Range("A24:C30").Select
    Selection.Copy
    ActiveWindow.ScrollRow = 4
    ActiveWindow.ScrollRow = 5
    ActiveWindow.ScrollRow = 6
    ActiveWindow.ScrollRow = 7
    ActiveWindow.ScrollRow = 9
    ActiveWindow.ScrollRow = 10
    Range("A31").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
    Selection.Copy

    Sheets("Save Sales").Select
    Range("B6").Select
    Selection.Insert Shift:=xlDown

    Sheets("Invoice").Select
    Application.CutCopyMode = False

    ActiveCell.FormulaR1C1 = ""
    Range("B9").Select
    MsgBox "Print Now"

End Sub