Results 1 to 9 of 9

Thread: WithEvents of Excel.Application Events

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10

    Another Short Example

    Another Short Example

    Taken from this Blog post . https://www.exceltip.com/events-in-v...excel-vba.html
    https://www.exceltip.com/events-in-v...#comment-50479

    This example will bring up a message box telling you which worksheet you selected
    05_ExcelTipExampleGig.jpg
    http://i.imgur.com/rNaYcFr.gif

    http://www.exceltip.com/wp-content/u...01/gif8-13.gif

    I will do the example assuming we want the macro to be enabled by the opening of the workbook. I will do just one possible version of the Class way and the Non Class way

    Class Way

    In the ThisWorkbook object code module:
    ClassWayExTipsThisWorkbookObjectCodeModule.JPG http://i.imgur.com/gxH1KEu.jpg

    Code:
    Private AppE As MyAppEvents
    Private Sub Workbook_Open()
     Set AppE = New MyAppEvents
    End Sub
    In a Class Module, which is given the name MyAppEvents
    ClassWayExTipsThisWorkbookClassCodeModule.JPG http://i.imgur.com/Iychi2a.jpg

    Code:
    Private WithEvents myApp As Excel.Application
    Private Sub Class_Initialize()
     Set myApp = Excel.Application
    End Sub
    Private Sub myApp_SheetActivate(ByVal Sh As Object)
     MsgBox ActiveWorkbook.Name & "-" & Sh.Name
    End Sub
    ClassWayExcelTipExample.xls : https://app.box.com/s/rb7f51emtsamv18pklz358ux6cyozfkl


    Non Class Way

    In the ThisWorkbook object code module:
    NonClasWayExTipsThisWorkbookObjectCodeModule.JPG http://i.imgur.com/l9SfDDV.jpg

    Code:
    Private WithEvents myApp As Excel.Application
    Private Sub Class_Initialize()
     Set myApp = Excel.Application
    End Sub
    Private Sub myApp_SheetActivate(ByVal Sh As Object)
     MsgBox ActiveWorkbook.Name & "-" & Sh.Name
    End Sub
    NonClassWayExcelTipExample.xls : https://app.box.com/s/hllpmw3h9p277rgs6mi2321u47k2uhtb
    Attached Files Attached Files
    Last edited by DocAElstein; 01-04-2021 at 06:56 PM.

Similar Threads

  1. How To Create Interactive Charts in Excel Using Chart Events
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 2
    Last Post: 04-18-2019, 03:32 PM
  2. Replies: 1
    Last Post: 09-29-2013, 12:02 AM
  3. Replies: 4
    Last Post: 07-10-2013, 04:35 AM
  4. Replies: 1
    Last Post: 02-14-2013, 11:08 AM
  5. Excel Application.OnKey With Parameter
    By Excel Fox in forum Excel Help
    Replies: 0
    Last Post: 11-29-2011, 01:31 PM

Posting Permissions

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