View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
SIGE SIGE is offline
external usenet poster
 
Posts: 206
Default Trigger macro when nr of sheets changes

Hi Bob,
It is working fine ...though still triggering with my hidden sheet upon
opening, while not any sheets added/deleted.

Here is the code...
Best Regards Sige

'***Workbook code:

Option Explicit

Dim NumSheets As Integer

Private Sub Workbook_Open()
SheetsShow True
NumSheets = ThisWorkbook.Sheets.Count
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
SheetsShow False
ThisWorkbook.Saved = True
ThisWorkbook.Save
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim sActive As Object
If ThisWorkbook.Sheets.Count < NumSheets Then
NumSheets = ThisWorkbook.Sheets.Count
MsgBox ActiveSheet.Name
End If
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Workbook_SheetActivate ActiveSheet
End Sub

'***Module:

Sub SheetsShow(blnState As Boolean)
Dim Sht As Worksheet

If blnState Then
For Each Sht In ThisWorkbook.Worksheets
Sht.Visible = xlSheetVisible
Next
Sheet2.Visible = xlSheetVeryHidden
Else
Sheet2.Visible = xlSheetVisible
For Each Sht In ThisWorkbook.Worksheets
If Sht.CodeName < "Sheet2" Then
Sht.Visible = xlSheetVeryHidden
End If
Next
ActiveSheet.Activate
End If

End Sub












Bob Phillips wrote:
Try this alternative solution, and let us know if it causes any other
problems

Dim NumSheets

Private Sub Workbook_Open()
NumSheets = ThisWorkbook.Sheets.Count
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim sActive As Object
If ThisWorkbook.Sheets.Count < NumSheets Then
NumSheets = ThisWorkbook.Sheets.Count
MsgBox ActiveSheet.Name
End If
End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Workbook_SheetActivate ActiveSheet
End Sub


The Purbecks clouded over :-))

Bob

(remove nothere from email address if mailing direct)

"Sige" wrote in message
oups.com...
Oops.

1.A bit too fast ... : When deleting a sheet ...it triggers ...but
only when clicking another sheet first.