Class Stuff: VBA Custom Classes & Objects, Class Modules
Administrative Notes: : this blog is not finished - this blog on class stuff to be finished next winter hopefully. April 2020 or later... pics in eileenslounge Nick basics
Class Stuff
Introduction To Classes
Class Stuff: VBA Custom Classes & Objects, Class Modules ( Custom Events)
.
Class Modules.
This is a bad start point. But it seems to be often peoples first experience with this “Class stuff”. Class modules are a small way down from the start of what it is all about, and for the sake of .. I don’t know what .. the subject is often started with the mechanics of setting up a class module. I think using this typical approach and start point seems to detract from someone getting the point.( Its as bit like explaining to someone in detail how to change a wheel on a car when he needs to know how to organize running a taxi company )
I think this results in that somebody trying to learn will .. not see the wood for the trees.
It is true that a class is created by inserting a Class Module (in your VBA project) to which you give a name, but that is just arbitrarily how the mechanics of it have been written.
So I won’t start the story there, by the inserting of a Class Module.
It is a better start point to say…” In VBA you can create your own custom objects by defining classes. Classes act as templates for new objects “
A brief introduction to objects and class objects in VBA. Object Orientated Programming,
If you understand a bit about Object Orientated Programming, OOP, then you will know that it is all about Objects, and an object can be a whole lot of diverse things. OOP is an annoyingly vague concept. The Objects tend to be organised hierarchically starting with the big ones at the top of the hierarchy, and going down through smaller ones.
http://www.excelfox.com/forum/showth...ange#post10809 , , https://imgur.com/tirYIdz http://i.imgur.com/tirYIdz.jpg
Something running parallel to this hierarchical structure which loosely fits into it all, is that we define a Class as a blueprint or Template from which to build one or more objects of that type , or model, or Class. Class is also a vague concept and means like a template, blueprint , or set of instructions or procedures to define how objects built from the Class will look like.
At this point in the discussions, the VB Editor starts getting very misleading: it does not represent clearly the actual structure that we have for two reasons:
_ the positioning of thing is not consistent
_ some things are simply not shown, ( possibly deliberately as we have no access to them. )
Example Model of a workbook
The Workbook
__________________________________________ThisWorkbook
Lets restrict ourselves for the sake of simplicity to having a single workbook as the “start” . Then we could consider a workbook class from which our single workbook is made
ThisWorkbookClassObject___ThisWorkbookObject.
What we see in the VB Editor left hand side explorer window is somehow a representation of the actual Excel object, ( or “instance” of it ) , that we have in front of us. The imprecise definition of objects allows us to consider the code module ( which appears when you double click on ThisWorkbook in the left hand VB Editor explorer window ) as part of workbook object.
In the next post we review the existing Class objects, which inspection of the left hand side explorer window of the VB Editor appears to indicate that we have some access to.
We will see that they are somewhere in between a normal macro code module and a Class module. They could possibly be described as object modules, or Class object modules
Having considered those two existing class related things, we will go on to discuss the other two class related things, which we will see are more close to a "pure" class thing. Those will be the things related to the classic "Class module" and the UserForm
https://web.archive.org/web/20180518...l/classes.aspx
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
https://eileenslounge.com/viewtopic.php?p=320960#p320960
https://eileenslounge.com/viewtopic.php?p=320957#p3209573
https://eileenslounge.com/viewtopic.php?p=318868#p318868
https://eileenslounge.com/viewtopic.php?p=318311#p318311
https://eileenslounge.com/viewtopic.php?p=318302#p318302
https://eileenslounge.com/viewtopic.php?p=317704#p317704
https://eileenslounge.com/viewtopic.php?p=317704#p317704
https://eileenslounge.com/viewtopic.php?p=317857#p317857
https://eileenslounge.com/viewtopic.php?p=317541#p317541
https://eileenslounge.com/viewtopic.php?p=317520#p317520
https://eileenslounge.com/viewtopic.php?p=317510#p317510
https://eileenslounge.com/viewtopic.php?p=317547#p317547
https://eileenslounge.com/viewtopic.php?p=317573#p317573
https://eileenslounge.com/viewtopic.php?p=317574#p317574
https://eileenslounge.com/viewtopic.php?p=317582#p317582
https://eileenslounge.com/viewtopic.php?p=317583#p317583
https://eileenslounge.com/viewtopic.php?p=317605#p317605
https://eileenslounge.com/viewtopic.php?p=316935#p316935
https://eileenslounge.com/viewtopic.php?p=317030#p317030
https://eileenslounge.com/viewtopic.php?p=317030#p317030
https://eileenslounge.com/viewtopic.php?p=317014#p317014
https://eileenslounge.com/viewtopic.php?p=316940#p316940
https://eileenslounge.com/viewtopic.php?p=316927#p316927
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
2 Attachment(s)
Review of the existing Class objects that we already see in the VB Editor
Review of the existing Class objects that we already see in the VB Editor
( Workbook , Worksheets )
Workbook
We are considering that there is a workbook class from which our single workbook is made
__ThisWorkbookClassObject_______ThisWorkbookObject.
What we see in the VB Editor left hand side explorer window is somehow a representation of the actual Excel object, ( or “instance” of it ) , that we have in front of us. The imprecise definition of objects allows us to consider the code module ( which appears when you double click on ThisWorkbook in the left hand VB Editor explorer window ) as part of workbook object.
It is most likely that what we “see” and can manipulate will be the right-hand side of those two things, that is to say,
________________________________ThisWorkbookObject
The Workbook
So after double clicking on ___ThisWorkbook _ in the left hand VB Editor explorer window, you can consider that part of what you have opened up is a small part of the right hand side thing in these two things
__ThisWorkbookClassObject_______ThisWorkbookObject
The left hand side you have no sight of usually and cannot have do anything to. This makes some sense. If you could get inside and fiddle around with that then you could change fundamentally how a workbooks works and in doing so cause all sorts of corruptions and problems. Also you could probably get to see a lot of secret proprietary information about Excel and Microsoft Office…
The “thing” we are considering here, and trying to make some sense of, is the large empty window, which is usually referred to as the ThisWorkbook “code module”.
I am suggesting that it can loosely be considered to be part of the workbook object.
A simple way to confirm the above ideas is to run this simple macro in the large ThisWorkbook. Code module/ window. ( If you have a non English Excel then the name may be different. For example in my German Excel it is DieseArbeitsmappe )
Code:
Option Explicit
Sub MeWb() ' What am I. What object am I
MsgBox prompt:=Me.Name ' This will give your File name
MsgBox prompt:=Me.CodeName ' This will give ThisWorkbook ( or your language equivalent ) It is this code module name
End Sub
The above macro will tell you the workbook name, and the code module name as shown in the VB Editor. So that gives you some indication that the ThisWorkbook “code module” which you have the macro placed in, is somehow part of the your workbook object.
Why do we have a ___ ThisWorkbook _ object code module
We can’t make a workbook object. We pay Microsoft for the software that does that. But they give us some limited**** access to the object coding..
In particular some macros already exist which are hooked on to events that occur. They are there all the time , and very slightly degrade performance , since they run automatically every time something is done. But the effect is minimal as no code lines are inside the procedures. But we are allowed to add code lines to them. It would probably be more easy to understand if those Procedures were shown already in the large code window, but for some reason it was decide to hide them. To revel them you select them via the left hand and right hand drop down lists at the top of the ___ThisWorkbook _ code module:
https://imgur.com/GU2Cq9Q https://imgur.com/UOuFaJd http://i.imgur.com/GU2Cq9Q.jpg http://i.imgur.com/UOuFaJd.jpg
Attachment 2828Attachment 2829
In that example in the last screenshot we could add coding which would be done every time the workbook was opened.
For example, this would give a message every time the workbook was opened
Code:
Private Sub Workbook_Open()
MsgBox Prompt:="Hello, you just opened the workbook"
End Sub
Note the typical syntax of events codes: There is typically a single underscore, _ , in the middle. This separates the object on the left hand side and on the right hand side the event.
__Sub TheObjectToWhichTheCodingApplies_TheEventType(_____________)
(Transgression , ( applicable here and to the next section on worksheet object code modules) : Note further, that , as we will later see, we can define any object variable, say , varObj , to “subscribe” to the events of an object. This means that that a variable representing an object, that is to say an object variable like varObj has the same access to all the events of the object to which it subscribes. We do this “subscribing”, for example , for the case of a variable, varObj like this:
Dim WithEvents varObj As Workbook
In this case we have “subscribed” varObj to the events of the Workbook Class
Having done this, we will now see the object variable appearing in the left hand drop down list along with Workbook,( https://imgur.com/gL1fvQ8 http://i.imgur.com/gL1fvQ8.jpg ) , and it has access to exactly the same event procedures as our Workbook
varObj Subscribed WithWorkbook.JPG : http://i.imgur.com/v0QCiYv.jpg
Important to note however, is that coding using the variable varObj will never work. It is not clear why this is so****. What we have done here in this slight transgression is to demonstrated the mechanics in place, in particular the use of WithEvents . The actual use of this WithEvents is discussed here: http://www.excelfox.com/forum/showth...ication-Events )
Normal coding and ThisWorkbook object module
Coding in the object module ran from within the module
We can add normal procedures, and they will also work within such a module. This fact is possibly an arbitrary decision by the makers of Excel of no significance… As the code module represents part of an actual object instance, the actual workbook, it makes possibly some sense that it can “do things”
For example, we could write a simple code to check our event code from above.
Code:
Option Explicit
Dim WithEvents varObj As Workbook ' ****
Sub TestOpenWbEventMacro()
Call varObj_Open
Call Workbook_Open
End Sub
Private Sub varObj_Open() ' ****Note: Micrrosoft have decided codes of this form will not react to the event
MsgBox prompt:="Hello, you just opened the workbook"
End Sub
Private Sub Workbook_Open()
MsgBox prompt:="Hello, you just opened the workbook using Private Sub Workbook_Open()"
End Sub
Coding in the object module ran from outside the module
(Methods)
We could also test the macro, using a macro in any other module. At this point it is another undocumented grey area to explain what is going on and why, that allows it to work…
_ You would first need to change the macros in the ThisWorkbook object code module to remove the restriction of them only accessible in that code module by changing the Private to Public:
Code:
Option Explicit
Dim WithEvents varObj As Workbook ' ****
Public Sub varObj_Open() ' ****Note: Micrrosoft have decided codes of this form will not react to the event
MsgBox prompt:="Hello, you just opened the workbook"
End Sub
Public Sub Workbook_Open()
MsgBox prompt:="Hello, you just opened the workbook using Private Sub Workbook_Open()"
End Sub
_ Having changed the coding in the ThisWorkbook code module above, it now appears as if the sub routines act as methods of the workbook, but there is no clear documentation to this. The following macro can be run from any code module.
Code:
Option Explicit
Sub TestOpenWbEventMacro()
Call ThisWorkbook.varObj_Open
Call ThisWorkbook.Workbook_Open
‘ The following are sometimes called methods of the
ThisWorkbook.varObj_Open
ThisWorkbook.Workbook_Open
End Sub
Variables ( Properties )
This is another grey area of understanding and definitions.
We can declare a simple variable at the top of the object code module. If we use Private or just Dim , it has the same effect, and the variable is only available in that module. In other words this coding must all go into the ThisWorkbook code module
Code:
Dim LsWkBkGlb1 As Long
Private LsWkBkGlb2 As Long
Sub PlayWithMeGlobiesInLisWkBk()
Let LsWkBkGlb1 = 1
Let LsWkBkGlb2 = 1
End Sub
If we change the declarations to Public like this…_
Code:
Public LsWkBkGlb1 As Long
Public LsWkBkGlb2 As Long
_.. then we will find that this will work in any code module
Code:
Sub PlayWithMeGlobiesInLisWkBk()
Let ThisWorkbook.LsWkBkGlb1 = 1
Let ThisWorkbook.LsWkBkGlb2 = 1
End Sub
The Public variables held in such a way in an object module are sometimes referred to as Properties of the object or as a form of global variable, that is to say a variable accessible in other modules. It is not clear and often experts argue as to what is going on here. As we will see later, a variable so declared via the class module way is fairly clearly defined as a property of an object that is made from the class module template, is fairly clearly defined then as a property of the finally made object. In the case of an object like ThisWorkbook, which already exists, the definition is less clear.
_._______________________
So in conclusion, it seems that we have a structure like the following, only some of which we have access to, and of that only some things are enabled to work: We have a single code module which probably fits into a structure of
______________________Excel Application
______________________Workbook Class
________[ClassModule]_____[ThisWorkbookObjectCodeModule]_Workbook.xl__
We have access to the object, ________[ClassModule]_____[ThisWorkbookObjectCodeModule]_Workbook.xl__
1 Attachment(s)
Review of the existing Class objects that we already see in the VB Editor
Review of the existing Class objects that we already see in the VB Editor
( Workbook , Worksheets
Worksheets
The Worksheets
__________________________________________WsCodeName(WstabName)
__________________________________________Sheet2(Sheet2)
__________________________________________Sheet3(MySht)
We find that the behaviour of the worksheet code modules, ( obtained by double clicking on their representation in the left hand Explorer part of the VB Editor ) , is very similar to that of the ThisWorkbook code module discussed in the previous section.
It suggest that once again there exists for each worksheet a higher up the hierarchy object , likely a Class object we have no access to, and Microsoft have allowed us just one instance of each of these worksheet classes. It is possible, and likely based on the general OOP ideas, that those Classes are themselves produced from a higher Class.
So a structure like this:
_________________Workbook.xl__
____________ Worksheets Collection Worksheet Class
____ Class Sheet1 ___ Class Sheet2 _____ Class Sheet3
_________ Sheet1 ___ _ _____Sheet2______ _____ Sheet3
Likely reason for the worksheet code modules existence: Worksheet Events.
These are the events codes most often encountered and often they are the only ones explained under the Theme of events codes. But in fact they are just a small selection of event codes whereby they have been partially already put in place in a way that only loosely ties up with some general order.
A example, consider a Worksheet object. This thing is simultaneously regarded as the spreadsheet of that worksheet or the worksheet code module. We do not have to make this: This has been pseudo done for us, and is somehow part of the Excel software which we purchased. When we run excel something pseudo of this form has been done , or is done, as the software starts up
_ A class object of a Worksheet has been made. Possibly one is made for each of the default worksheets that appear. (The number may vary depending on your version of Excel ) So there exist somewhere a very complicated class object and/or class object module , or modules, which we can’t see with the name Worksheet.
_ A few worksheet objects are made from the classes. For the case of a worksheet, they are “subscribed” to the Events associated with a worksheet. Internally some variables, say for the case of the first worksheet, Sheet1 , will be used pseudo like. ( These pseudo code lines are a geuss of what happens internally , and of which we cannot influence).
Dim WithEvents Sheet1 As Worksheet
_ Set Sheet1= New Worksheet
Briefly, those two lines do the following:
the Dim gives some initial indication of the type of thing that Sheet1 refers to. This might mean referring to the text/ Blueprint info of the variable type of setting aside a copy of such. In any case it does not apparently require too much computer memory. The variable state, for the case of an object, is typically regarded as Nothing , at this point.
It is usually considered that a more substantial computer memory is used as a result of Set and New pair. This goes by the name of “instantiation” or similar and is regarded as “making” or “building” the object.
The exact coding and resulting structure of objects is not clear, that is to say not given openly, and it is not permitted to do those steps in coding for the case of a Worksheet!!!. We are not allowed to instantiate a Worksheet. It is this coding which we are suggesting possible results in some pseudo structure of like
_________________Workbook.xl__
____________ Worksheets Collection Worksheet Class
____ Class Sheet1 ___ Class Sheet2 _____ Class Sheet3
_________ Sheet1 ___ _ _____Sheet2______ _____ Sheet3
It is not clear or disclosed as which of the lower two levels we actually “see” in the code window or the spreadsheet. We see something of, and have some access to , the Sheet1, Sheet2, Sheet3 type “things”. Sometimes the lower two levels might be considered as the same. Sometimes the code module might be considered as the Class Sheet1 and the spreadsheet as Sheet1.
So it is all a bit Unclear and open to interpretations.
Consider some points:_
_ General coding will work in the worksheet code modules in addition to the Event codes. ( We will see later that general coding does not work in the Class module object which we can create).
_ We cannot subscribe a variable to the events of a Worksheet, or rather we are permitted to do it from the syntax, in a similar way to which we considered for the Workbook, but the resulting codes simply do not work!!!….
_ We do not need to do the following to step through a code placed in such a module via stepping through a coding manually in debug ( F8 ) mode:
Tools Options.JPG : https://imgur.com/KZUyPnB , http://i.imgur.com/KZUyPnB.jpg
Break in class modules.JPG : https://imgur.com/75Eg6UE , http://i.imgur.com/75Eg6UE.jpg
So those three facts might suggest that we are at the lower level … better described as an object than a class module
These findings and conclusions are consistent with those from the Workbook considerations, as are the next findings which are identical to the ThisWorkbook object code module
Normal coding and the worksheet object code module object module
Coding in the object module ran from within the module
We find that we can add normal procedures, and they will also work within such a module. This fact is possibly an arbitrary decision by the makers of Excel of no significance… As the code module represents part of an actual object instance, an actual worksheet, it makes possibly some sense that it can “do things”
For example, we could write a simple macro in a worksheet object code module , simple to that we did in the ThisWorkbook object code module, to help support our ideas about what the object code modules are..
Code:
Option Explicit
Sub WhatAmI()
MsgBox prompt:=Me.Name
MsgBox prompt:=Me.CodeName
End Sub
MyWkSht Sheet3.JPG : https://imgur.com/bBBhOZm http://i.imgur.com/bBBhOZm.jpg
Attachment 2830 Attachment 3526
The above macro, when run, will tell us out tab name and the name in the VB Editor explorer window , which is typically referred to as the code name. As before this seems to give us the approximate idea that the code module in which the macro is is part of the actual worksheet. It is not 100% clear, as is typically the case for things to do with objects in OOP.
Coding in the object module ran from outside the module
(Methods)
Once again things works as in the previous object code module, that is to say a macro such as the previous considered can be run from any module with a macro like the following. The macro being called is sometimes considered as a Method of the worksheet object, but once again it is not a 100% clear definition.
Code:
Sub CallWhatAmIMethodOf_Sheet3()
Call Sheet3.WhatAmI
Sheet3.WhatAmI
End Sub
Variables ( Properties )
We can declare a simple variable at the top of the object code module, as we did in the case of the ThisWorkbook object code module. If we use Private or just Dim , it has the same effect, and the variable is only available in that module. In other words this coding must all go into the worksheets object code module. ( I am using the last example macro slightly differently in this form for no particular reason – Just another way to demonstrate the use of the variables )
Code:
Option Explicit
Dim WsGlob1 As String
Private WsGlob2 As String
Sub WhatAmI()
Let WsGlob1 = Me.Name
MsgBox prompt:="The Tab name of this worksheet is " & WsGlob1
Let WsGlob2 = Me.CodeName
MsgBox prompt:="The code name of this worksheet is " & WsGlob2
End Sub
If we change the declarations to Public like this…_
Code:
Option Explicit
Public WsGlob1 As String
Public WsGlob2 As String
_.. then we will find that this will work in any code module
Code:
Sub WhatAmI() ' In any code module
Let Sheet3.WsGlob1 = Worksheets("MyWhSht").Name
Let Sheet3.WsGlob1 = Sheet3.Name
MsgBox prompt:="The Tab name of this worksheet is " & Sheet3.WsGlob1
Let Sheet3.WsGlob2 = Worksheets("MyWhSht").CodeName
Let Sheet3.WsGlob2 = Sheet3.CodeName
MsgBox prompt:="The code name of this worksheet is " & Sheet3.WsGlob2
End Sub
The Public variables held in such a way in an object module are sometimes referred to as Properties of the object or as a form of global variable, that is to say a variable accessible in other modules. It is not clear and often experts argue as to what is going on here. As we will see later, a variable so declared via the class module way is fairly clearly defined as a property of an object that is made from the class module template, is fairly clearly defined then as a property of the finally made object. In the case of an object code module belonging to any of the worksheets which was somehow already made for us, the definition is less clear. We are adding coding which could be considered to be from the blue print, but which obviously can’t be, since Microsoft didn’t know exactly what coding we were going to put in it: There is no direct blue print for it. Possibly something is in place in the custom class that is hidden from us, that allows for this. Or it might just be a coincidence, or by product, that we can do it, as Microsoft may have used the same basic coding to get this code window showing as they did to get the normal code window showing. So something’s will happen to work similarly as a result of this, independently to anything else and not directly related to any Class concepts.
Summary in words, existing Class objects that we already see, and what we don’t see, in the VB Editor
Short summary in words, existing Class objects that we already see, and what we don’t see, in the VB Editor
We can call almost anything an object. The concept of an object is very vague.
The Objects tend to be organised hierarchically starting with the big ones at the top of the hierarchy, and going down through smaller ones. But once again its not always strictly held to.
Something running parallel to this hierarchical structure which loosely fits into it all, is that we define a Class as a blueprint or Template from which to build one or more objects of that type , or model, or Class. Class is also a vague concept and means like a template, blueprint , or set of instructions or procedures to define how objects built from the Class will look like.
Class things will tend to be further up the hierarchical structure, but once again, that is a general idea or concept. There is no point in wasting time arguing the toss about being accurate about some think that is a vague concept left to an individuals interpretation, but based on certain concepts which I have outlined
There are “Class level”, Class objects that we don’t have any access to. They are probably extremely complex and define a lot of what an instance of Excel will be. At this level , or approximately we may view it as such, we can have a custom Class. We will see in the following posts that in effect that means we can insert and view a code module, used to define the object made from the “blue print” that the coding in a class module is.
For the case of the “Class level”, Class objects that we don’t have any access to, we can see the objects: We can argue that Excel itself and the Worksheets are those, but also the object code modules are parts of those: For example, you can look inside a worksheet object code module. ( For example, double click on it in the VB Editor to open it : http://i.imgur.com/9iTEib0.jpg : ) - That is a “real” object code module . Its made by Microsoft using a Class module that we can’t see and one we cant get direct access to it.
With our home made Class module it’s the other way around. We can see the class module. But the “real one” or “real ones” that gets made, we can’t see. ( I am not sure why that is. Maybe Microsoft just wanted it that way )
I think a lot of the documentation and Blog sites don’t have it quite correct. They often refer to the ThisWorkbook and the worksheet code modules as class modules. I don’t think they are. They are object modules made form class modules we don’t have direct access to.
We can argue the toss about why things are so organised. But no one knows for sure, and experts usually get upset when you talk about it**., since they prefer to know what they are talking about , which they don’t , half the time.
The class modules we can’t see are likely massive with a lot proprietary information. We will see that we are limited to a small number of coding types in our custom Class module.
The access to the object code modules is possibly primarily to allow us to fill in coding within the available event codings.
This event coding consideration is not directly relevant to our Custom Class situation: We don’t have a simple way to make custom events. ( This Theme is handled in a more complex way ( https://excelfox.com/forum/showthrea...ication-Events ) )
Ref
** https://www.myonlinetraininghub.com/...-module#p19192
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgzMCQUIQgrbec400jl4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwhVTFaD469mW9wO194AaABAg. 9gJzxwFcnPU9gORqKw5tW_
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugyb8nmKKoXvcdM58gV4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgwvvXcl1oa79xS7BAV4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=UgxvIFArksPprylHXYZ4AaABAg
https://www.youtube.com/watch?v=f7xZivqLZxc&lc=Ugxq4JHRza_zx3sz0fx4AaABAg
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
https://eileenslounge.com/viewtopic.php?f=27&t=35521&p=276185#p276185
https://eileenslounge.com/viewtopic.php?p=276185#p276185
https://eileenslounge.com/viewtopic.php?p=276185#p276185
https://eileenslounge.com/viewtopic.php?p=276673#p276673
https://eileenslounge.com/viewtopic.php?p=276751#p276751
https://eileenslounge.com/viewtopic.php?p=276754#p276754
https://eileenslounge.com/viewtopic.php?f=30&t=35100&p=274367#p274367
https://eileenslounge.com/viewtopic.php?p=274368#p274368
https://eileenslounge.com/viewtopic.php?p=274370#p274370
https://eileenslounge.com/viewtopic.php?p=274578#p274578
https://eileenslounge.com/viewtopic.php?p=274577#p274577
https://eileenslounge.com/viewtopic.php?p=274474#p274474
https://eileenslounge.com/viewtopic.php?p=274579#p274579
https://www.excelfox.com/forum/showthread.php/261-Scrolling-Marquee-text-on-Userform?p=864&viewfull=1#post864
https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
Custom Classes and Custom objects. General and UserForms
Custom Classes and Custom objects. General and UserForms
We are approximately ¾ into the story and have reached the part which touches on the area often at the start of traditional explanations of these things. The previous discussions told us that we, when using Excel, are somewhere down from the true Class level, having access to objects already built from a Class which we had no direct access to.
Excel VBA does allow us to start at the top, all be it with a limited possibilities in what we can achieve and do. We have two possibilities.
The first starts with being able to add a code module which is at the level of a Class. This is misleadingly seen in the VB Editor as similar to the other modules, whereas its pseudo up a level , and is intended to be a Blue print / Template from which objects are then instantiated.
To confuse us further , those instantiated objects , would have a similar place in the OOP hierarchy as those two object types discussed which we can see. But the object code modules we can effectively make from our custom class module, are not represented in the VB Editor.
Having already made a confusing mess, as is typical with Microsoft, they like to take it further, and these unrepresented objects can be many, since we are free to instantiate as many objects from the Class as we choose.
The second, a UserForm is a slightly different thing altogether, and we will only discuss some basics of this in particular things relevant to our general Class discussions, for completeness. ( For anyone familiar with the “ stand alone “ Form/Window applications of traditional Visual Basic, the UserForm is very approximately a similar idea for a semi independent Window type application , but within Excel , rather than anywhere within Windows, as with the traditional “ stand alone “ Form/Window of Visual Basic )
Before discussing , further , some general notes specifically concerning the usefulness of using Class / Class modules, in particular the use of Class modules, ( not necessarily directly related to UserForms )…
Some general discussions about the ( non ) usefulness of using Class / Class modules
_ There is not much, if anything that can be done with Class Modules that can’t be done with normal VBA Functions. There is little if any, performance advantages. There is no increase in functionality or efficiency
_ For somebody writing a lot of complicated coding it can help that person to both organise and later remember what you were doing. Related coding can be kept in the same place, and it helps make the coding self documenting. At the same time it makes the coding more difficult for someone else to follow and understand. So it is a useful tool to help you organise complicated long coding, and if you want to share and make it a bit more difficult for someone else to understand.