|
|
|
|
|
|
Excel
|
How to use Triggered Events for all Workbooks with an Add-In
|
|
Ease of Use
|
Hard
|
Version tested with
|
2002
|
Submitted by:
|
Jacob Hilderbrand
|
Description:
|
This is an example of how to create a class of every workbook that you have open. You can then use triggered events in each workbook with an Add-In.
|
Discussion:
|
There are Worksheet and Workbook level events, but there is no built in Application level event. In some cases you may want to use a Triggered Event in every Workbook with an Add-In. This is an example of how to do that.
|
Code:
|
instructions for use
|
Option Explicit
Private Sub Workbook_Open()
Call ClassInitialize
End Sub
Option Explicit
Dim Wkbs() As New WorkBookClass
Dim App As New App
Public Sub ClassInitialize()
Dim i As Long
Dim Wkb As Workbook
For Each Wkb In Application.Workbooks
i = i + 1
ReDim Preserve Wkbs(1 To i)
Set Wkbs(i).WkbBook = Wkb
Next
Set App.App = Application
End Sub
Option Explicit
Public WithEvents App As Application
Private Sub App_NewWorkbook(ByVal Wb As Workbook)
Call ClassInitialize
End Sub
Option Explicit
Public WithEvents WkbBook As Workbook
Private Sub WkbBook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
MsgBox "Triggers are working properly:" & vbNewLine & _
"Workbook Name: " & WkbBook.Name & vbNewLine & _
"Worksheet Name: " & Sh.Name & vbNewLine & _
"Range Address: " & Target.Address(False, False)
End Sub
|
How to use:
|
- Open Excel.
- Alt + F11 to open the VBE.
- Double Click on the ThisWorkbook code module and add the appropriate code from above.
- Insert a Standard Module and add the appropriate code from above.
- Insert two Class Modules and name them App and WorkBookClass and add the appropriate code from above. Make sure that the Class Modules are named correctly and that the correct code is put into each one.
- Close the VBE (Alt + Q or press the X in the top right corner).
- Save the workbook as an Add-In.
- Close the workbook.
|
Test the code:
|
- Open a workbook.
- Tools | Add-Ins...
- Browse for and install the Add-In.
- Type something into a cell and the code will trigger.
- Add a new workbook and type something into a cell in the new workbook and the code will still trigger.
|
Sample File:
|
AppWorkbookWorksheetEvent.ZIP 8.65KB
|
Approved by mdmackillop
|
This entry has been viewed 188 times.
|
|