On 2002-04-17 05:36, Prosenjit Banerjee wrote:
I have to run some code whenever the user deletes a worksheet. i.e. I want to trap an event (if any) of deleting the sheet. But, I could not found any such event. What should I do? Please help me.
These is no such event for this...but you could
try this work around;
<pre/>
Dim shName As String
Dim Avail
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
On Error Resume Next
Avail = Sheets(shName).Range("A1")
If Err Then
MsgBox shName & " has been Deleted ...Put your routine here to run?"
End If
On Error GoTo 0
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
'Application.DisplayAlerts = False
shName = Sh.Name
'Application.DisplayAlerts = True
End Sub
'This works on the fact that the Workbook_SheetDeactivate event is triggered before
'the SheetActivate event and stores the Sheet name in variable shName.
'The sheetActivate event runs a little routine that
'tries to get a value from the lastsheet, which is
'shName (from the DeactivateEvent), the on Error Resume next is put there so that if the sheet no longer
'exists it generates an error object which is evaluated.
'THIS IS WHERE you can put your routine you want
'to run when a sheet is deleted.
</pre>