![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Location: India
Posts: 19
|
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.
|
|
|
|
|
|
#2 | |
|
MrExcel MVP
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
|
Quote:
These is no such event for this...but you could try this work around; 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. |
|
|
|
|
|
|
#3 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Kobe, Japan
Posts: 1,420
|
Hi.
If you do not need the name of deleted sheet, pls copy this into a ThisWorkbook module and try this.
[ This Message was edited by: Colo on 2002-04-17 23:16 ] |
|
|
|
|
|
#4 |
|
New Member
Join Date: Jun 2009
Posts: 2
|
Hello,
What about before deleting the worksheet ? Thanks |
|
|
|
|
|
#5 |
|
MrExcel MVP
Moderator They call me "Mary" Join Date: May 2008
Location: In the flat field
Posts: 16,253
|
I think you would have to intercept all possible ways of deleting the sheet. Can I ask what the purpose is?
__________________
Rory Microsoft MVP - Excel. My wife treats me like a god - she ignores my existence until she wants something. |
|
|
|
|
|
#6 |
|
New Member
Join Date: Jun 2009
Posts: 2
|
Hello,
I have a workbook with let's say two spreadsheets, one will allow deletion and the other one no. So if the user chooses the tab and right click on it I do not want to show the DELETE option for the spreadsheet. In the worst case scenario, the delete option is there but somehow the "DELETE event" is risen and the application validates which sheet is deletable and which not. I tried to "protect" the spreadsheet, but then I can not insert or delete rows/columns in that spreadsheet. Thanks. |
|
|
|
|
|
#7 | |
|
Board Regular
Join Date: Dec 2002
Location: Larache--Morocco
Posts: 4,570
|
Quote:
If you have Excel 2000 or later then you can prevent the deletion of all the worksheets or a specific worksheet as follows : This will prevent the deletion of Sheets("A") - Place the code in ThisWorkbook module : Code:
Private WithEvents cmbbtnEvents As CommandBarButton
Private Const TargetSheet As String = "A" '//change this sheet as required.
Private Sub cmbbtnEvents_Click _
(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
If Ctrl.ID = 847 Then
If ActiveSheet Is Sheets(TargetSheet) Then
CancelDefault = True
MsgBox "Can't delete sheet " & Sheets(TargetSheet).Name
End If
End If
End Sub
Private Sub Workbook_Open()
Set cmbbtnEvents = Application.CommandBars.FindControl(ID:=847)
End Sub
|
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|