本文介绍了VBA代码不一致使Excel崩溃(运行时错误80010108)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下VBA代码将一张纸上的一行自动移动到另一张纸上.大约有1/3的时间,它给出了运行时错误'-2147417848(80010108)",然后使Excel崩溃.我找不到一个常见的原因.发生崩溃后,我可以在同一行上执行相同的代码,下一次它可能会正常运行,也可能无法正常运行.

I'm using the following VBA code to automate moving a row on one sheet to another. About 1/3 of the time, it gives a "run-time error '-2147417848 (80010108)" and then crashes Excel. I cannot find a common reason why. After a crash, I can execute the same code on the same row, and it may or may not work fine the next time.

谁能说出下面这段代码为什么不稳定?

Can anyone tell why this code below should be unstable?

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

推荐答案

您需要完全限定您的行.看到这个例子.

You need to fully qualify your Rows. See this example.

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

这篇关于VBA代码不一致使Excel崩溃(运行时错误80010108)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 15:04