Run Time Error 9 on Deleting sheet

mgCulver

New Member
Joined
Dec 2, 2018
Messages
19
In trying to delete a sheet, I constantly get run-time error 9. I think the reason is because the code called to delete the sheet originates from within a button on the sheet; kinda-sorta like suicide prevention.

To Try to avoid this, I placed code in the activate event of another sheet and created a separate module with code to delete the sheet. However, this still generates the same error.

Any suggestions would be helpful.

Thx,

Mike

Code in the activate event of a sheet:

If ActiveSheet.Name <> "Sand Box" Then
If DelSheet = True Then
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
On Error Resume Next
Application.DisplayAlerts = False
Call DeleteSheet(SheetToDelete)
'Sheets(SheetToDelete).Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True

DelSheet = False
SheetToDelete = ""
Sheets("Sand Box").Visible = False
Else
'Do Nothing
'Sheets("Sand Box").Visible = False
End If
Else
End If

Code in

Sub DeleteSheet(strSheetName As String)
'On Error GoTo 0
'Application.DisplayAlerts = False
'Sheets(strSheetName).Select
Sheets(strSheetName).Delete
'ActiveWindow.SelectedSheets.Delete
'ActiveSheet.Select
'Range("A10").Select
'Application.DisplayAlerts = True
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You haven't set a sheet name to delete in this code ???
So the macro doesn't know which sheet to delete

Code:
Sub DeleteSheet(strSheetName As String)
'On Error GoTo 0
'Application.DisplayAlerts = False
'Sheets(strSheetName).Select
Sheets(strSheetName).Delete
'ActiveWindow.SelectedSheets.Delete
'ActiveSheet.Select
'Range("A10").Select
'Application.DisplayAlerts = True
End Sub
 
Upvote 0
Hi Michael.

A sheet name is passed to the activate event of a sheet; in turn that calls the sub (Call DeleteSheet(SheetToDelete)) and passes the sheet name to the sub. 'strSheetName' is the variable.

If I step through the code, it does receive the correct name.

The fail occurs when I the sheet is deleted on this line of code - Sheets(strSheetName).Delete

Thx
 
Upvote 0
Ok, where have you set strSheetName as a variable in either code ??
 
Upvote 0
Are you trying to delete ALL sheets except "sandBox"
or jusr another sheet ?
What is the name of that other sheet ?
 
Upvote 0
This Workbook allows a user to create multiple budgets/actuals for various activities for my client. Once they have completed the information on the sheet, it is posted to a history file and charted. When the data is posted, I need to delete the sheet. So the name of the sheet is variable depending on how the client names it on a user form. There can be as few as one sheet active at a time, or multiple sheets.

'SheetToDelete' is defined as a public variable. Therefore it is available through all sheets and changes when the user posts the data to the history file. The deletion routine does not need to have strSheetName defined as a variable as the sub is passed the sheet name from the call. [Call DeleteSheet(SheetToDelete); Sub DeleteSheet(strSheetName As String)].

Apparently this problem did not occur in Excel 2007 and prior; I am writing this in Excel 2013.

Thx
 
Upvote 0

Forum statistics

Threads
1,223,970
Messages
6,175,707
Members
452,667
Latest member
vanessavalentino83

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