Worksheet will delete from userform...Why?

malik641

Board Regular
Joined
Sep 6, 2005
Messages
65
I keep getting runtime errors while trying to delete a worksheet that was specified by the userform. Here is the code:

Code:
Private Sub OKButton_Click() 
     'On Error Resume Next
    Application.ScreenUpdating = False 
    Dim WS          As Worksheet 
    Dim EmpName     As Variant 
    Dim cell        As Variant 
    Dim NextName    As Range 
    Dim i           As Long 
    i = 0 
    Set NextName = Range("Hide_Sheets").End(xlDown).Offset(1, 0) 
     
     'Actions based on which option selected - Delete or Hide
     '**************************************************************************  *********
    If OptionDelete Then 
        Application.DisplayAlerts = False 
        For Each WS In ThisWorkbook.Worksheets 
            If WS.Name = EmpCombo.Value Then 
                shName = WS.Name 
                Worksheets(shName).Delete 'Delete Method of Worksheet Class Failed
                Worksheets(WS.Name).Delete 'Delete Method of Worksheet Class Failed
                WS.Delete 'Method Delete of object '_Worksheet' failed
                 
                 'Goes to DeleteExcess_SheetName function to take out the name of the worksheet
                 '  from the Employees list
                Call DeleteExcess_SheetName 
                Unload EmpUserForm 
                Exit Sub 
            End If 
        Next WS 
         
        Application.DisplayAlerts = True 
    End If 
     '**************************************************************************  *********
    Unload EmpUserForm 
    Application.ScreenUpdating = True
End Sub
I shortened the code for relevancy, but if you need me to post the whole code for the OK button, let me know and I'll do so.

Under the OptionDelete, I've placed a couple of tries I had at deleting the worksheet. I placed the error I get right after them. I've tried these methods with a blank workbook and they worked fine, so what gives?? ??

Thanks in advance!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
The two "Delete Method of Worksheet Class Failed" and the one "Method Delete of object '_Worksheet' failed" errors. I placed them to the right of the posted code of where I tried to delete the sheets.
 
Upvote 0
How about just:
Code:
        For Each ws In ActiveWorkbook.Worksheets
            If ws.Name = EmpCombo.Value Then ws.Delete
        Next ws
HTH,

Smitty
 
Upvote 0
I got it. The workbook was protected and I forgot to unprotect it.

As for pennysaver's submission....I don't know why but that method wasn't working for me (I guess because of other dependent code)...but it's working fine the way I have it now. Thanks again!!
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,933
Members
449,480
Latest member
yesitisasport

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top