PDA

View Full Version : Use Triggered Event in all workbooks



Mark_online
10-27-2010, 08:38 AM
I have a sheet-specific triggered even procedure (actually it came from the certification training course) and I would like to implement it across all of my Excel workbooks. I'm also interested in knowing how to to this in general with things other than macros. Here is the triggered event procedure:


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim Row As Long
Dim Col As Long
Dim Rng1 As Range
Dim Rng2 As Range

Application.EnableEvents = False
Row = Target.Row
Col = Target.Column
Set Rng1 = Range(Cells(Row, 1), Cells(Row, Col))
Set Rng2 = Range(Cells(1, Col), Cells(Row, Col))
Cells.Interior.ColorIndex = 0
Rng1.Interior.ColorIndex = 6
Rng2.Interior.ColorIndex = 6
Application.EnableEvents = True

End Sub

I, and some colleagues would like to have that appear in all of our current and future Excel workbooks with an easy way to disable it if necessary. Thanks.

Bob Phillips
10-27-2010, 09:38 AM
Private WithEvents app As Application

Private Sub app_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim nRow As Long
Dim nCol As Long
Dim Rng1 As Range
Dim Rng2 As Range

Application.EnableEvents = False
nRow = Target.Row
nCol = Target.Column
Set Rng1 = Sh.Range(Cells(nRow, 1), Cells(Row, Col))
Set Rng2 = Sh.Range(Cells(1, nCol), Cells(Row, Col))
Sh.Cells.Interior.ColorIndex = 0
Rng1.Interior.ColorIndex = 6
Rng2.Interior.ColorIndex = 6
Application.EnableEvents = True
End Sub

Private Sub Workbook_Open()
Set app = Application
End Sub


This is workbook event code.
To input this code, right click on the Excel icon on the worksheet
(or next to the File menu if you maximise your workbooks),
select View Code from the menu, and paste the code