MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Apr 17th, 2002, 06:36 AM   #1
Prosenjit Banerjee
New Member
 
Join Date: Mar 2002
Location: India
Posts: 19
Default

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.
Prosenjit Banerjee is offline   Reply With Quote
Old Apr 17th, 2002, 10:40 PM   #2
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
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;



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.





__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old Apr 18th, 2002, 12:16 AM   #3
Colo
MrExcel MVP
 
Colo's Avatar
 
Join Date: Mar 2002
Location: Kobe, Japan
Posts: 1,420
Default

Hi.
If you do not need the name of deleted sheet, pls copy this into a ThisWorkbook module
and try this.


Dim cntSh As Integer

Private Sub Workbook_Open()
cntSh = Sheets.Count
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
If Sheets.Count < cntSh Then Call test(cntSh - Sheets.Count)
cntSh = Sheets.Count
End Sub

Private Sub test(cntShNow As Integer)
If cntShNow = 1 Then
MsgBox cntShNow & " sheet has been Deleted"
Else
MsgBox cntShNow & " sheets has been Deleted"
End If
End Sub


[ This Message was edited by: Colo on 2002-04-17 23:16 ]
Colo is offline   Reply With Quote
Old Jun 11th, 2009, 03:21 AM   #4
saavedrah
New Member
 
Join Date: Jun 2009
Posts: 2
Default Re: Worksheet-DELETE event???

Hello,

What about before deleting the worksheet ?

Thanks
saavedrah is offline   Reply With Quote
Old Jun 11th, 2009, 04:35 AM   #5
rorya
MrExcel MVP
Moderator
They call me "Mary"
 
rorya's Avatar
 
Join Date: May 2008
Location: In the flat field
Posts: 16,253
Default Re: Worksheet-DELETE event???

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.
rorya is offline   Reply With Quote
Old Jun 11th, 2009, 09:04 PM   #6
saavedrah
New Member
 
Join Date: Jun 2009
Posts: 2
Default Re: Worksheet-DELETE event???

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.
saavedrah is offline   Reply With Quote
Old Jul 4th, 2010, 01:58 AM   #7
Jaafar Tribak
Board Regular
 
Jaafar Tribak's Avatar
 
Join Date: Dec 2002
Location: Larache--Morocco
Posts: 4,570
Default Re: Worksheet-DELETE event???

Quote:
Originally Posted by saavedrah View Post
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.

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
__________________
Common sense is not so common.

http://www.laracheenelmundo.com/
Jaafar Tribak is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 04:52 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes