Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How to listen to "rename sheet"

Hi

I'd like to create a listener on the event "sheet.name changing". It
apparently does not exist in vba worksheet standard callbacks.
(said in a different way I want to run a function any time a specific
sheet has its name manually changed)
Any idea ? Any help would be appreciated.

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default How to listen to "rename sheet"

Changing a sheet name does not fire any events. Why do you need to know when
a sheet name has been changed? Perhaps there is a way around this.
--
HTH...

Jim Thomlinson


"Ben" wrote:

Hi

I'd like to create a listener on the event "sheet.name changing". It
apparently does not exist in vba worksheet standard callbacks.
(said in a different way I want to run a function any time a specific
sheet has its name manually changed)
Any idea ? Any help would be appreciated.

Thanks


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default How to listen to "rename sheet"

Ben,

There is no event triggered when a sheet is deleted or renamed. The closest
you can get, I think, it to keep an array of existing sheet names and then
compare that list to the actual sheet names in a SelectionChange event. Put
the first code block in a class named CSheetChange and the second block of
code in the ThisWorkbook module. ThisWorkbook will sink events raised by
CSheetChange when a sheet is detected to have been added, deleted, or
renamed.


'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
' CSheetChange Class
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
Option Explicit

Private WithEvents WB As Workbook
Private Arr() As String
Public EnableEvents As Boolean

Public Event AfterSheetNameChange(OldName As String, NewName As String)
Public Event AfterSheetDelete(OldName As String)
Public Event AfterSheetAdd(NewName As String)

Public Property Set TheWorkbook(WBook As Workbook)
Dim N As Long
Set WB = WBook
With WB.Sheets
ReDim Arr(1 To .Count)
For N = 1 To .Count
Arr(N) = .Item(N).Name
Next N
End With
End Property



Private Sub Class_Initialize()
Me.EnableEvents = True
End Sub

Private Sub WB_NewSheet(ByVal Sh As Object)
Dim N As Long
With WB.Sheets
ReDim Arr(1 To .Count)
For N = 1 To .Count
Arr(N) = .Item(N).Name
Next N
If Me.EnableEvents = True Then
RaiseEvent AfterSheetAdd(Sh.Name)
End If
End With
End Sub

Private Sub WB_Open()
Dim N As Long
With WB.Sheets
ReDim Arr(1 To .Count)
For N = 1 To .Count
Arr(N) = .Item(N).Name
Next N
End With
Me.EnableEvents = True
End Sub



Private Sub WB_SheetActivate(ByVal Sh As Object)
WB_SheetSelectionChange Sh, Nothing
End Sub

Private Sub WB_SheetDeactivate(ByVal Sh As Object)
WB_SheetSelectionChange Sh, Nothing
End Sub

Private Sub WB_SheetSelectionChange(ByVal Sh As Object, ByVal Target As
Range)

Dim N As Long
Dim M As Long
With WB.Sheets
If .Count = UBound(Arr) Then
''''''''''''''''''''''''''''''
' Same number of sheet. check
' for rename.
'''''''''''''''''''''''''''''
For N = 1 To .Count
If StrComp(Arr(N), .Item(N).Name, vbBinaryCompare) < 0 Then
If Me.EnableEvents = True Then
RaiseEvent AfterSheetNameChange(Arr(N), .Item(N).Name)
End If
Arr(N) = .Item(N).Name
Exit Sub
End If
Next N
ElseIf .Count < UBound(Arr) Then
'''''''''''''''''''''''''''''''
' Sheet has been deleted.
'''''''''''''''''''''''''''''''
For N = 1 To .Count
If StrComp(Arr(N), .Item(N).Name, vbBinaryCompare) < 0 Then
If Me.EnableEvents = True Then
RaiseEvent AfterSheetDelete(Arr(N))
End If
For M = N To .Count
Arr(M) = .Item(M).Name
Next M
ReDim Preserve Arr(1 To .Count)
End If
Next N
End If
End With

End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
' END CSheetChange
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''


'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
' ThisWorkbook Module
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''

Option Explicit

''''''''''''''''''''''''''''''''''''''''''''''''
' Declare the class variable WithEvents so we
' can get events.
''''''''''''''''''''''''''''''''''''''''''''''''
Private WithEvents SheetChanger As CSheetChange

Public Sub SetupChanger()
'''''''''''''''''''''''''''''''''''''''
' Setup SheetChanger
'''''''''''''''''''''''''''''''''''''''
Set SheetChanger = New CSheetChange
Set SheetChanger.TheWorkbook = Me
SheetChanger.EnableEvents = True
End Sub

Private Sub SheetChanger_AfterSheetAdd(NewName As String)
MsgBox "After Sheet Add:" & vbCrLf & _
"Name: " & NewName
End Sub

Private Sub SheetChanger_AfterSheetDelete(OldName As String)
MsgBox "After Delete: " & vbCrLf & _
"Old Name: " & OldName
End Sub

Private Sub SheetChanger_AfterSheetNameChange(OldName As String, NewName As
String)
MsgBox "After Name Change:" & vbCrLf & _
"Old Name: " & OldName & vbCrLf & _
"New Name: " & NewName

End Sub


Private Sub Workbook_Open()
SetupChanger
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
' END ThisWorkbook
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"Ben" wrote in message
ups.com...
Hi

I'd like to create a listener on the event "sheet.name changing". It
apparently does not exist in vba worksheet standard callbacks.
(said in a different way I want to run a function any time a specific
sheet has its name manually changed)
Any idea ? Any help would be appreciated.

Thanks


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
Excel - Golf - how to display "-2" as "2 Under" or "4"as "+4" or "4 Over" in a calculation cell Steve Kay Excel Discussion (Misc queries) 2 August 8th 08 01:54 AM
Excel "Move or Copy" and "Delete" sheet functions dsiama Excel Worksheet Functions 1 December 28th 07 01:57 PM
Change default in input box from "20" to "000000"? Have macro continue on to name sheet tab? StargateFanFromWork[_4_] Excel Programming 7 October 29th 06 11:46 AM
Please add a "sheet" function like "row" and "column" functions Spreadsheet Monkey Excel Programming 2 November 8th 05 04:08 PM
How to use macro to rename worksheet with date +"text" Sherri[_3_] Excel Programming 4 April 28th 04 12:05 AM


All times are GMT +1. The time now is 10:12 PM.

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"