运行时440 Excel错误在VBA中删除所选行

时间:2017-03-28 19:00:06

标签: vba excel-vba excel-2010 excel

编辑:所以我尝试在此代码中手动执行操作,并且excel也崩溃了。这是否意味着我遇到了与其他海报相同的崩溃错误?

作为序言,公平警告我不是一名专业程序员,我只是玩这个试图让我(和我的同事)生活更轻松。

这个代码工作一个月前,当我创建它时,可能它被更新破坏了,不确定,但我不确定为什么它现在抛出错误。我所拥有的另一个非常相似的宏仍然有效。

现在会抛出错误440 ,之后,MS Excel会崩溃。

Sub RemoveRow()
    Dim Eng As Worksheet
    Dim rmg As Range
    Dim Sup As Worksheet

    Application.ScreenUpdating = False

       '~~> Set this to the relevant worksheet
    Set Eng = ThisWorkbook.Sheets("Engr+Tech")
    Set Sup = ThisWorkbook.Sheets("Suprv+Sppt")

    Eng.Activate

With Eng

    Range("D8").End(xlDown).Select

    '~~> Set your range
    Set rmg = .Rows(ActiveCell.Row)

    '~~> Delete the range
    rmg.Delete

    '~~> Select last row
    Set rmg = .Rows(ActiveCell.Row)
    rmg.Offset(-1).Select

    '~~> Select techs
    Cells(Selection.Row, 4).End(xlDown).Select
    Cells(Selection.Row, 4).End(xlDown).Select

     '~~> Set your range
    Set rmg = .Rows(ActiveCell.Row)

    '~~> Delete the range
    rmg.Delete

    '~~> Select last row
    Set rmg = .Rows(ActiveCell.Row)
    rmg.Offset(-1).Select

    '~~> Select ftechs
    Cells(Selection.Row, 4).End(xlDown).Select
    Cells(Selection.Row, 4).End(xlDown).Select

     '~~> Set your range
    Set rmg = .Rows(ActiveCell.Row)

    '~~> Delete the range
    rmg.Delete

End With

Sup.Activate

With Sup

    Range("D8").End(xlDown).Select

    '~~> Select CLS
    Cells(Selection.Row, 4).End(xlDown).Select
    Cells(Selection.Row, 4).End(xlDown).Select

     '~~> Set your range
     '~~> Set rmg = .Rows(ActiveCell.Row)

    '~~> Delete the range
    .Rows(ActiveCell.Row).Delete

End With

Eng.Activate

Range("A1").Select

Application.ScreenUpdating = True


End Sub

2 个答案:

答案 0 :(得分:2)

尝试不使用所有选择/激活

Sub RemoveRow()

    Dim Eng As Worksheet
    Dim rmg As Range
    Dim Sup As Worksheet

    Application.ScreenUpdating = False

    '~~> Set this to the relevant worksheet
    Set Eng = ThisWorkbook.Sheets("Engr+Tech")
    Set Sup = ThisWorkbook.Sheets("Suprv+Sppt")

    '>> avoiding deleting the cell referenced by rmg...
    Set rmg = Eng.Range("D8").End(xlDown).Offset(-1, 0)
    rmg.Offset(1, 0).EntireRow.Delete

    Set rmg = rmg.End(xlDown).End(xlDown).Offset(-1, 0)
    rmg.Offset(1, 0).EntireRow.Delete

    Set rmg = rmg.End(xlDown).End(xlDown).Offset(-1, 0)
    rmg.Offset(1, 0).EntireRow.Delete

    Sup.Range("D8").End(xlDown).End(xlDown).End(xlDown).EntireRow.Delete

    Eng.Activate
    Eng.Range("A1").Select

    Application.ScreenUpdating = True

End Sub

答案 1 :(得分:1)

非常感谢帮助我完成这一切的所有人。它从3月17日开始成为一个安全补丁,新补丁(今天发布)纠正了这个问题。

这是我得到的更新: https://support.microsoft.com/en-us/help/3178690/ms17-014-description-of-the-security-update-for-excel-2010-march-14-20

解决方案是更新到新补丁: https://support.microsoft.com/en-us/help/3191855