This is post https://www.excelfox.com/forum/showt...age2#post24185
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms/page2#post24185
https://www.excelfox.com/forum/showt...ll=1#post24185
https://www.excelfox.com/forum/showthread.php/2965-Class-related-Stuff-Userforms?p=24185&viewfull=1#post24185
An attempt at an initial summary on UserForm (module)
We are really talking about Class things and class ideas. Inevitably to get some understanding you must accept/ appreciate / be aware of, the hierarchical Object Orientated Programming concept that VBA is modelled on: Very simplified: we start, as it were towards the top, or top left, with classes and from those objects are made and they finally have Properties. The final objects with their properties ( and methods etc.), could be crudely be regarded as some "actual" or significant "things", whereas the class is just the details of how to make the objects, or a template to be used over and over again and added to or filled in appropriately to suit the particular instance like a classic simple example,
_ The concept of a car could be regarded as a Class,
_ a particular model as an instance of it,
_ and a color would be a property.
( _ A method might
The VB Editor
The VB editor is a tool we use , often referred to as the development environment, that , amongst other things, by default ,allows us to conveniently look at and develop/ change our objects. It is almost impossible not to use objects from a class if you are using Excel. By default we have limited direct access to any classes other than to make objects from them, assign variables to them, or modify the properties of the objects made from them.
But there are two sorts of Class that we can create, and we can use the VB Editor to do that as well
Use Keys Alt+F11 from Excel to get the VB Editor
, or alternatively
, right click on any worksheet tab and select view code, - https://i.postimg.cc/J04LpnsW/Right-...eet-coding.jpg
Attachment 5865
,That will show you initially a representation of the Excel objects associated with your file, which usually by default will be for the workbook itself, ThisWorkbook, and the worksheets you have. (If you took this second approach to get the VB Editor then initially the big window will be for any coding you may add for the worksheet who’s tab you selected.)
https://i.postimg.cc/nh69N8KW/Big-wi...u-selected.jpg
Attachment 5866
We are for now not interested in the existing things. They are associated with existing Excel objects.
It is important to realise that most object things in Excel follow the object orientated programming ideas, which for us in the current discussions means they were made from referring to some sort of blueprint / template / set of written instructions etc. We refer to these blueprint / template / set of written instructions etc. as a Class.
For most of the existing Excel objects, we have only limited, if any, direct access to the Class. That is because it will be propriety information allowing you to construct Excel, so trade secrets as it were.
We are interested in the available adding of a New Class UserForm. Associated with that UserForm Class thing is one of two class related module types that Microsoft makes available to us.
Get one with a bit of right clicking near the representation of your Excel file in the left hand VBA project window of the VB Editor,
https://i.postimg.cc/GmZzK54L/Do-a-b...-Form-opti.jpg , https://i.postimg.cc/kgVfxmXY/Do-a-b...orm-option.jpg
Attachment 5867 , Attachment 5868
You should then see the basic tools of
_ a simple pin board looking box or "form", usually with the default name of UserForm1 ,( both for the main class thing and the caption name on the pin board thing top left , ( more to that later ** ) )
, and
_ a Toolbox dialog should also be visible. (If it’s not visible select View->Toolbox from the VB editor top ribbon menu. We use the toolbox to add controls to our UserForm.)
The bottom left window should also now show the properties related to this UserForm
https://i.postimg.cc/W3Fgbm8z/User-F...-VB-Editor.jpg , https://i.postimg.cc/852WFFXH/User-F...-VB-Editor.jpg
https://i.postimg.cc/N9t5dHWv/User-F...-VB-Editor.jpghttps://i.postimg.cc/Q9qVwM39/User-F...-VB-Editor.jpg
I will change the Name property to suit the previous discussions, to ufResults , ** , and note that the Caption did not change,
https://i.postimg.cc/FzN5szZd/Captio...User-Form1.jpg
https://i.postimg.cc/Mc0QJw2B/Captio...User-Form1.jpg
, and we will leave it like that for now, as this will help along the way, to demonstrate a quirk with UserForm Class things, which was brought out by our discussions … Unlike normal classes, UserForms are auto-instantiating, so you can simply call them by name and a new instance of the class is created. ….. as you use ufResults for the first time, a new instance of the form is created.
It's confusing because you effectively get a variable of the same name as the class.
This last statement we will come back to after getting the basics behind us now.
Important summary of what we have so far.
So we have a class now. Parts of this class is the added UserForm module. The name ufResults is effectively the name of the greater thing that is the Class. Exactly what that greater thing is, is deep in the innards of Excel and Office, it concerns how the thing eventually works , and is anyone’s guess in the meantime what / where it is. We are only interested in the available interface that we see, which we can use to determine what sort of object can be made from it. We are only concerned with tools and interface given to us to help shape how the blueprint / template / set of written instructions etc finally "looks". But note what we see and finally have is not much more significant than an instruction manual. Itself it will not "do anything". We use it to make the ( or as many as we like ) final object(s) that "does" something.
Generally a class is designed to be used over and over again, just like a template.
What is a class- what we have so far
As we said previously , it’s a blueprint / template / set of written instructions
In simple layman beginner terms its like a piece of paper in your pocket with some notes and instructions on how to build an object of this type / class. Apart from the information on it, as a thing its pretty insignificant in itself.
The whole point of the class module is just to make it more convenient to have the text / instructions / explaining diagrams etc. conveniently for you to get at in Excel and use automatically / programmatically, and thereby automatically build an object from, rather than having to painstakingly read from the bit of paper and manually construct the thing and manually add coding to it etc.
A main difference between A UserForm class module, and the other standard Class module, is that we have that main extra box with grid points on it, and other bits and pieces in the tool box, like empty lists and boxes, to make it a bit easier to fill in. In simplest layman terns, the difference between,
_ For a normal class module being like a blank piece of paper . Mainly you would put the text of coding you were interested in for you final object, on it. (Part of that typical coding text is there for you to select and add to, to save you a bit of time typing)
, and
_ The UserForm is similar, but like a bit of Graph paper along with some other pre prepared bits and pieces to stick on it. Associate with most of those things are then also available part of the text of coding that might be typically associated with those things, there for you to select and add to, to save you a bit of time typing