2

我正在使用以下 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
4

2 回答 2

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
于 2012-08-30T20:39:50.267 回答
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
于 2016-06-30T19:05:42.217 回答