does an event get triggered when you delete a sheet?

pliant

Board Regular
Joined
Jan 8, 2003
Messages
238
is there any event triggered when you delete a sheet?

when a user deletes a sheet i would also like to delete that sheet from a list of sheets that i have on another sheet...
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
I do not know of an event that is triggered when a sheet is deleted.

There may be another way to accomplish what you are looking for though. Can you explain why you need to delete the sheet name from a list of names?
 
Upvote 0
How about code in the Sheet_Activate event of the sheet with the list that will iterate through the sheets in the workbook and generate a fresh list?
 
Upvote 0
i have a list of items that populates a combo box and a separate sheet for each item.

if the user deletes a sheet i would like that item removed from the combo box as well so the user cannot select it.
 
Upvote 0
Put this code in the ThisWorkbook module in a blank workbook with 2 sheets:

Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    MsgBox Sh.Name & " activated"
End Sub

Then delete a sheet. The event is triggered.

You can replace the MsgBox statement with the code to update your list of sheets.
 
Upvote 0
I think I've figured it out. This seems to work:

'get the name of the sheet being deactivated
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
sheetName = Sh.Name
End Sub

'check if it is still there when the next sheet is activated
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim sheetCount As Integer
Dim found As Boolean

sheetCount = Sheets.Count

For i = 1 To sheetCount
If Sheets(i).Name = sheetName Then
MsgBox ("sheet found")
found = True
End If
Next

If Not found Then
MsgBox ("sheet NOT found!")
'delete from list
End If

End Sub
This message was edited by pliant on 2003-01-17 13:17
 
Upvote 0
This worked for me:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim x As Integer
    Dim WS As Worksheet
'   *** Sheet containing list of worksheets - change to suit ***
    Set Sh = Worksheets("Sheet1")
'   *** Range containing worksheet names - change to suit ***
    Set Rng = Sh.Range("A2:A" & Sh.Range("A2").End(xlDown).Row)
    For x = Rng.Rows.Count To 1 Step -1
        On Error Resume Next
        Set WS = Worksheets(Rng.Cells(x, 1).Text)
        If Err <> 0 Then
            Rng.Cells(x, 1).EntireRow.Delete
        End If
    Next x
End Sub

It assumes that your list of worksheets starts in A2 on Sheet1 - change to suit.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,576
Members
448,972
Latest member
Shantanu2024

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top