Results 1 to 10 of 30

Thread: Class Stuff: VBA Custom Classes & Objects, Class Modules

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #19
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,446
    Rep Power
    10
    This is post https://www.excelfox.com/forum/showt...ll=1#post24163 https://www.excelfox.com/forum/showt...age3#post24163




    Some Extra notes to go with this main forum post
    https://eileenslounge.com/viewtopic....ffd8b7#p317533

    That forum post is one of those increasing occurrences recently of a one off post answering a question. There relevance to the Thread and their quality varies a lot. This one was fairly relevant but more of a quick example files perhaps picked up from somewhere, and I doubt we will ever see the OP again and I doubt he knows much about what he posted.
    But it gave me a chance to try and get some Class ideas of mine a bit better understood


    It is probably helpful to download the uploaded file, and have it open it in Excel, whilst working through the next few posts





    First what is it all about
    This appears to be an example of my suggested second of two main areas that class things may be used …_ 2 The area where I have mostly seen …… This usually involves tackling the area of WithEvents ….


    I started with a new virgin simple .xls file, and put a Class Module in it
    Add a Class Module.JPG

    I went through copying across your ( OP SamPi ) coding and changing it a bit and adding some 'comments to help me explain what was going on.
    (By the way, What actually happens (should happen) is that …
    Provided you first open the main file with all the class stuff in it, ( that was your (OP SamPi) OpenExcelFile.xlsm , and it is now my WatchMyFileOpenings.xls), then after that , if you open a file with the name test.csv , then, a message box should pop up and tell you about it )


    First the Class module stuff.
    This is my slightly modified version of your (OP SamPi) coding put inside the Class module.
    I gave the class module the name FileOpenWatcher , but that does not mean much. Initially, before we put text of coding into it, its just like a blank sheet of paper.
    Finally as below, it’s not much more. It is just like text on a bit of paper. Whilst not completely incorrect, it might be dangerous to envisage this as a template. This might cause one to think of it as a thing to be copied and used over and over again. Later with hindsight, this conception can be misleading. It is better to think of it as some notes in your pocket on a piece of paper, that you work from to make something over and over again. Think of there being other text notes and information, which we personally do not concern ourselves with, and when we use all the information, the text of the coding is just one thing we use. Generally it is a bad idea to consider copying of a class or coding in a class module. It can be misleading to the actual underlying concepts.
    The class module is generally designed to be used over and over again, in the similar way that a function is. In this example we only use it once. Here is the full text. Perhaps that text could be thought better conceptually as like that on a Stamp


    Class module, FileOpenWatcher: ( https://i.postimg.cc/TPPmLX2K/File-O...ode-module.jpg ) of WatchMyFileOpenings.xls
    Code:
    ' This is not coding that will ever be done. It is like a stamp from which we effectively make the coding to put in an actual class object module:  This is a Class module. Not an actual class object module
    Option Explicit
    Public WithEvents ExApp As Excel.Application ' This makes ExApp a variable/ object that some how is like a running Excel, and also the   WithEvents  means it has access to all those  codings that it has that kick off when something happens
    
    ' This and the next macro is the text of coding we need to tell us if we open the file "test.csv"
    Private Sub ExApp_WorkbookOpen(ByVal Wb As Workbook) ' This will become a property in a final Instanciated object, amnd I intend to use it
    Dim s As String
     Let s = Wb.Name
        If s = "test.csv" Then Call MyMacro
    End Sub
    Sub MyMacro() ' This is available also in intellisense to a final instanciated object, although in this example I will not use it, - its calles from the routine above. Splitting the two is not done for any particular reason
    MsgBox Prompt:="You just opened  test.csv"
    End Sub

    That coding is not actual coding that will ever be done. It is like a blue print, or a Stamp , from which an actual object module will be made with that coding in it, (by an instanciate later in coding in another code module –
    In other words, something looking just like that, could be considered to appear, Stamped out as it were, by executing the following code lines
    Dim MeWatcher As FileOpenWatcher
    Private Sub Workbook_Open() ' This is done when this file opens
    Set MeWatcher = New
    We must image this appearing, as we do not see it)
    The full coding(text) window shown above is in a true Class module, as I tried to explain in my previous post , so its just like text on a piece of paper, and on its own pretty useless.

    Lets explain the build up of that text (which will later be coding):
    The very first line, Public WithEvents ExApp As Excel.Application , does something along the lines of making the variable, ExApp ,somehow be of a type like the Excel you are using, but not specifically the actual one you are using.
    The extra WithEvents means it gets all the usual Excel event codings so we can tap into them, ( and for convenience, after you make this line we get them extra things listed in the drop down list of the Class module
    WithEvents of the Excel application.JPG

    We have chosen the event coding that kicks in when a workbook is opened, as that is what we need for our particular example
    As generally with event coding, it can be considered that it is always there and runs when the event occurs ( the event of opening of a workbook in this example ), but we can add coding into it, so that this added coding also kicks in when the event occurs ( the event of opening of a workbook in this example )

    In this case the coding is fairly simple, to do a message box pop up, when a file opens with the name test.csv
    Code:
    ' This and the next macro is the sort of coding we need to tell us if we open the file "test.csv"
    Private Sub ExApp_WorkbookOpen(ByVal Wb As Workbook) ' This will become a property in a final Instanciated object, amnd I intend to use it
    Dim s As String
     Let s = Wb.Name
        If s = "test.csv" Then Call MyMacro
    End Sub
    Sub MyMacro() ' This is available also in intellisense to a final instanciated object, although in this example I will not use it, - its calles from the routine above. Splitting the two is not done for any particular reason
    MsgBox Prompt:="You just opened  test.csv"
    End Sub
    

    So that’s it for the class module. It’s only half the full story. The second half is in the next post

    A last important thing again to remember: This is just like a piece of paper with a text of coding on it that will never run itself.
    We may "copy" that text and put it somewhere so as to use as "real" coding to be run.
    The "copy", is effectively done by these 2 sort of code lines later,
    Dim MeWatcher As FileOpenWatcher
    Private Sub Workbook_Open() ' This is done when this file opens
    Set MeWatcher = New

    The use of "copy" is perhaps a bit unwise, better is to imagine it stamped out

    With that we are finally doing is instantiating: A class describes the variables, properties, procedures, and events of an object (but a class is not an object itself; an object comes into existence when a class is instantiated)
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by DocAElstein; 06-16-2024 at 01:19 AM.

Similar Threads

  1. PQ - IP C class generator
    By sandy666 in forum ETL PQ Tips and Tricks
    Replies: 0
    Last Post: 10-22-2020, 05:16 AM
  2. Backup all modules, class modules and userforms to a selectable folder
    By MrBlackd in forum Excel and VBA Tips and Tricks
    Replies: 1
    Last Post: 04-06-2014, 08:33 AM
  3. Manipulate VBA Array Object Using Class Module
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 0
    Last Post: 06-06-2013, 07:53 PM
  4. Array Class Module
    By Rajan_Verma in forum Rajan Verma's Corner
    Replies: 3
    Last Post: 12-20-2012, 11:22 AM
  5. Class Objects Created Using the CreateObject Method That Employs Late Binding
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 08-16-2011, 12:38 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
  •