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
    9,414
    Rep Power
    10
    Event coding in Excel spreadsheets(Application Events) Misc

    Post 4: “Class events” Using the “Class way” to do it

    There are a couple of pre subscribed and pre Instantiated event code added to a “Class module” after we insert a Class module.
    They basically kick in when an object is built ( instantiated ) using the blueprint of the Class and when an object from the Class is Set to Nothing
    A good way to understand their use which also helps understand the encapsulation, Encapsturbation , ideas, as well as the general use of Classes, particularly with even codes, is to consider re writing the event codes example of the last post in the way more preferred by computer professionals.
    The main difference in the two ways we will see is that we use one of the two pre subscribed and pre Instantiated event, and must then also do some other things to cause it to kick off. ( We will see that we cannot easily set this code off directly )
    Here is that code again, ( slightly modified to allow it to go in any object code module).
    The code assumes you have a worksheet with tab name “Sht_1”. If you change the value in the first cell of that worksheet, “Sht_1” , then you will get a message.

    Points to remember:…_
    _.. You must run the routine to instantiate WsSht_1 ( Sub InstantiateWsSht_1() in last post ) once to instantiate the object WsSht_1
    _.. There is no significance to the underscore, _ , in Sht_1 or WsSht_1 etc.. These are names we can choose. But as we have learnt, there is a significance to the _ and the SheetChange combination in _SheetChange , as this is a reserved events code character combination )
    Events _SheetChange Worksheet change.jpg : http://i.imgur.com/6NY0WeC.jpg
    Code:
    Dim WithEvents  WsSht_1 As Excel.Application
    Sub Instantiate____() ' Routine to Instantiate WsSht_1
     Set WsSht_1 = Excel.Application
    End Sub
    Sub WsSht_1_SheetChange(ByVal Sh As Object, ByVal Target As Range)
     If Sh.Name = "Sht_1" Then
     If Target.Address = "$A$1" Then MsgBox prompt:="You just changed the value in the first cell in worksheet """ & ActiveSheet.Name & """ in the Workbook """ & ActiveSheet.Parent.Name & """"
     Else '
     End If
    End Sub

    Summary : This is generally what we want:
    We want to make an object which we will name arbitrarily WsSht_1 . This is to be subscribed to the event codes of the current “open in front of us” Excel. In one of the event routines then available to this object, or rather already then there in this form:…._
    Code:
    Sub  WsSht_1_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    
     End Sub
    _... we will add coding so that it looks now like this:_......
    Code:
    Sub  WsSht_1_SheetChange(ByVal Sh As Object, ByVal Target As Range)
       If Sh.Name = "Sht_1" Then
         If Target.Address = "$A$1" Then MsgBox prompt:="You just changed the value in the first cell in worksheet " & Me.Name & " in the Workbook " & Me.Parent.Name
       Else '
       End If
    End Sub
    Hence after running the instantiating code to “build “ the variable ( WsSht_1 ) , “into an object” , that object will “monitor” or “watch for” changes in a sheet, and take the action determine by our added coding.


    _.___
    Last edited by DocAElstein; 01-02-2021 at 10:51 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

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
  •