0
votes

I am trying to delete a sheet selected from a list box. The msgbox commands displaying the sheet names are correct but I seem to get an error which is as below:

Runtime Error 1004: Delete method of range class failed.

Here is my code.

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
1
you have three lines that use the delete method, and you could not be bothered to tell us which line is failing .... and the last command is a msgbox command ..... since you said that the msgbox commands display the correct sheet names, no none of the delete methods in the code are failing. ... your problem is someplace else - jsotola
The problem is in the second to last line - there you use Delete method on Range object. - Michał Turczyn
Any autofilter or sheet protection on your "Report" sheet? - Tim Williams

1 Answers

1
votes

Try this:

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

instead of:

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

(second to last line).