【问题标题】:Error while deleting sheet. Runtime Error 1004 : delete method of range class failed删除工作表时出错。运行时错误 1004:范围类的删除方法失败
【发布时间】:2017-11-24 06:15:50
【问题描述】:

我正在尝试删除从列表框中选择的工作表。显示工作表名称的 msgbox 命令是正确的,但我似乎收到如下错误:

运行时错误 1004:范围类的删除方法失败。

这是我的代码。

If ListBox2.Selected(i) Then
        Worksheets(ListBox2.List(i)).Unprotect "asdf"
        Worksheets(ListBox2.List(i)).Delete
        MsgBox ListBox2.List(i)
        sht = ListBox2.List(i) + "_Graph"
        Worksheets(sht).Unprotect "asdf"
        MsgBox sht
        Worksheets(sht).Delete
        Worksheets("Report").Rows(ListBox2.ListIndex + 2).Delete
        MsgBox "KC " + ListBox2.List(i) + " Removed!"

    End If

【问题讨论】:

  • 您有三行使用delete 方法,您不必费心告诉我们哪一行失败了……最后一个命令是msgbox 命令…… .. 由于您说 msgbox 命令显示正确的工作表名称,因此代码中的 delete 方法都没有失败。 ...您的问题在其他地方
  • 问题出在倒数第二行 - 你在 Delete 对象上使用了 Delete 方法。
  • 您的“报告”工作表上有任何自动过滤器或工作表保护吗?

标签: vba excel


【解决方案1】:

试试这个:

Worksheets("Report").Rows(ListBox2.ListIndex + 2).EntireRow.Delete

代替:

Worksheets("Report").Rows(ListBox2.ListIndex + 2).Delete

(倒数第二行)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2015-11-04
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多