PC Review


Reply
Thread Tools Rate Thread

Detecting When User Deletes a Sheet

 
 
boyd.james@gmail.com
Guest
Posts: n/a
 
      20th Jul 2007
Is any event raised when a sheet gets deleted? I'd like to remove a
control button upon certain sheets being removed.

Thanks,

James

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SkxhdGhhbQ==?=
Guest
Posts: n/a
 
      20th Jul 2007
When a worksheet is deleted by user action, when it is deleted another sheet
becomes the active sheet. That causes the
Workbook_SheetActivate() event to fire. You could put code in that routine
to see if the sheet(s) you're interested in still exist in the book or not.
Of course, the Workbook_SheetActivate() event is going to fire at other
times: any time a new sheet is selected by the user.

Code might look something like this - although someone else may have a
better way/event to us, and perhaps an even faster test to see if the
sheet(s) still exists.

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim anyRange As Range
'just so you can see when it fires
MsgBox "SheetActivate Fired"

On Error Resume Next
'can refer to any cell on the sheet, since all you
'want to know is if that sheet is still in the
'workbook or not
Set anyRange = Worksheets("SheetOfInterest").Range("A1")
If Err <> 0 Then
Err.Clear
MsgBox "Sheet Does NOT Exist"
'the sheet does not exist
'code here to deactivate/remove your button
'but remember that this sheet is never going
'to exist again after this action is done
'
'could exit now or fall through to test for
'other sheets in similar fashion
On Error GoTo 0
Exit Sub
End If
MsgBox "Sheet is still in the workbook"
Set anyRange = Nothing 'release used resource
On Error GoTo 0 ' clear error trapping when all done

End Sub


"(E-Mail Removed)" wrote:

> Is any event raised when a sheet gets deleted? I'd like to remove a
> control button upon certain sheets being removed.
>
> Thanks,
>
> James
>
>

 
Reply With Quote
 
=?Utf-8?B?cnViZW4=?=
Guest
Posts: n/a
 
      20th Jul 2007
No, there is not any specific event, BUT you can achieve that with a little
effort:
* In ThisWorkbook declare a module variable to hold the last deactivated sheet
* do little programming in SheetDeactivate and SheetActivate events to
verify the last deactivated sheet is still alive:

code sample in ThisWorkbook:

Private mOldSheetName As String

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim oldFound As Boolean
Dim ws As Worksheet
oldFound = False
For Each ws In Worksheets
If ws.Name = mOldSheetName Then
oldFound = True
Exit For
End If
Next
If Not oldFound Then
Debug.Print "Sheet deleted: " & mOldSheetName
End If
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
mOldSheetName = Sh.Name
End Sub



"(E-Mail Removed)" wrote:

> Is any event raised when a sheet gets deleted? I'd like to remove a
> control button upon certain sheets being removed.
>
> Thanks,
>
> James
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Charting Macro That deletes Sheet =?Utf-8?B?RGF2ZV9UaG8xOTY4?= Microsoft Excel Misc 3 15th Oct 2007 09:02 PM
Re: Detecting when a user deletes a row Jim Rech Microsoft Excel Programming 5 18th Jan 2006 04:26 PM
Detecting when a user deletes a row Wescotte Microsoft Excel Worksheet Functions 0 8th Nov 2005 12:01 AM
Detecting when Excel user inserts or deletes a row =?Utf-8?B?d2pld2VsbA==?= Microsoft Excel Programming 3 2nd Nov 2005 09:02 AM
links from other sheet deletes formulas =?Utf-8?B?bmVkYTU=?= Microsoft Excel Misc 0 31st Aug 2005 05:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:48 AM.