Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Detecting When User Deletes a Sheet

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

Thanks,

James

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,365
Default Detecting When User Deletes a Sheet

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


" 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 31
Default Detecting When User Deletes a Sheet

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



" 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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Detecting when a user deletes a row Wescotte Excel Programming 7 January 18th 06 04:26 PM
Detecting when a user deletes a row Wescotte Excel Worksheet Functions 0 November 8th 05 12:01 AM
Detecting when Excel user inserts or deletes a row wjewell Excel Programming 3 November 2nd 05 09:02 AM
Detecting user idle time R Avery[_2_] Excel Programming 1 May 3rd 05 06:34 PM
Detecting when user deselects an add-in mark Excel Programming 4 April 27th 05 09:51 PM


All times are GMT +1. The time now is 07:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"