Consulting

Results 1 to 2 of 2

Thread: Use Triggered Event in all workbooks

  1. #1

    Use Triggered Event in all workbooks

    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.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    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
    [/vba]

    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
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •