Class Modules Explained in Simple English (with examples)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello there and welcome in this video I'm gonna do something that hasn't been done many times before I'm going to attempt to explain class modules in plain English that anyone can understand as well as explaining class modules I'm going to show you some practical examples of how they can be used in a practical real-world situation so class modules are the equivalent of classes that are used in many languages such as C sharp Java and Python so why do we use them in very simple terms we want our code to look less like this and we want it to look more like this so when our code is like a collection of blocks it means that each one is like a black box that is inputs and outputs but we don't really know what goes on inside and we don't really care we just wanted to do what it says on the tin in other words if it's like a collection in VBA we wanted to add items and we want it to give us back the items that we've added so in VBA we have objects like the workbook the worksheet the range the collection and so on these are created from classes or class modules but we don't have access to the source code class modules in VBA allow us to create our own objects a class module is simply a module that contains all the code when we use the plasma dual in our code we call it an object let's take a look at the VBA collection as it provides a great example of an object we use the VBA collection to store multiple items we can add an item we can get the count of the items in the collection and we can retrieve the item that we've added so let's ignore any other functionality of the collection just to keep it simple we create a collection object using the new keyword we can then have different data using the add function of the collection and we can retrieve the data item using the item function and we can get the number of items using count so the key point here is that we don't have access to the code of the collection we don't know how the code works under the hold and we don't care as far as we were concerned and it can stay a black box as long as it continues to work as we expected what we really care about is the behavior that is the adding retrieving and getting the totals item works as expected now you can see that adding a collection to our cold is simple we don't have to paste a bunch of code that has the functionality of the collection and then hammer it into shape to fit what we're doing we can simply add the collection by using new when we create our own class modules we have to add them to the world pot but that's quite simple the following is a list of advantages or having our code like blocks number one we don't have to keep rewriting the same code over and over it is easy to add code such as functionality to other projects by just dropping in the class object and we can update the code in the class without having to worry about it causing problems elsewhere in the code so BD advantage of using class modules is the new keyword and this allows us to create as many objects as we like while the code is running so it gives us a lot of flexibility so for example imagine we're reading records and we want to store customers first in Massillon when all the customers are from a certain country but we don't have to know in advance how many customers we have with that criteria we can just use set a new to create the object each time we find a matching country and this allows us to deal with any number of Records and it makes our code very flexible so let's create a class module as this is a really excellent way of understanding them so we right-click on our project select insert and class module and you can see a new class module appears in our project window now if we want to change the name we go to the properties window now if these aren't available you can just select them from the view menu so view properties window and view project window so we're going to create a member of our class and it's going to be first name so simply just a string that holds the first name and then we're going to have last name which again is a string so we're keeping these as simple as possible so that you can see exactly class modules work so now we're back in a standard module and we're going to create our class modules here so we use the new keyword and this creates a new object based on our class module CLS customer and you can see we have a second one so each time we're creating a new object in memory and this object is based on the class module CLS customer now that might sound complicated but as you'll see how they work is quite simple so we set the first name for each one and when we go and print out these first names you'll see that each one has a different first name so each object is different and that's the key point that you can understand here so let's run this code and as expected you'll see that we got the two different first names so we got John and Jenny so this kind of gives you an understanding of how the task modules work and the difference between a class module and an object but let's take another look and let's create one more just to make it crystal clear so we create a turret object and we set this to have a different name and when we print it out again you're gonna see that we're going to have three names so this kind of really illustrates the point that these are three different objects so this is quite different to how other Softworks in VBA so you can see the tree names that appeared so let me show you one more simple example and after this dem we'll get on to some kind of practical real-world examples of where I've used excel vba objects so this time we're just going to set the first and the last name and then I'm going to create a sub in the class and what the sub in the class does is it returns us the full name so this is very simple example but it really illustrates how we can use class objects so we create our unite soap and all we're basically doing is we're gonna print the first name and the last name and we'll go back then to the main module you can see that when we run this code what we're going to get back is the full name so this is like a very simple very simple piece of code but it's very useful because every time we change the name and we run unites it will give us back the correct name for those pieces of data now in the next section I'm going to look at two real-world examples of how I've used clasp modules so don't worry about these too much because they are kind of advanced but the whole purpose is just to illustrate the benefits of using classes and then when I do further videos we'll get back to looking at simple examples like I have here okay so let's have a look at our first practical use of a class so in this case we're going to extend the collection object in VBA and what that means is we're going to use the function of this class and then add our own functionality the problem with collection is it doesn't have its own sort function or reverse function and so on so what we end up having to do is to go to a website like my own for example getting the sort function here and copying it to the code now a problem with this is that there's no real connection with the collection and this code so it's not always obvious if this exists in an existing project so you see we've pasted it in here and now we can just use quicksort to call it but as I say it's a bit messy because it's kind of mixing a collection with some random functions that are not related to each other so we're going to create another function here just debug print and the purpose of the debug print is just so that we can print out the items so you can see here when you ran the code that it actually does the sort okay and this is fine as we would expect but what we want to do is we want to put this into our own class so how we do it is we basically create a class and this is called EMM collection now don't worry so much about the code but what I'm basically doing is take an advantage of the add item and count functions of collection so just using the same ones and then adding my own from such as reversed sort and debug print so we can see how we can use them in the court here we basically just declare as an EMM collection and then you can see any time we use call our new functions appear like debug print so this makes it very obvious when we're using these that sort is available reverses available and so on and you can see it's also neater because we don't have to keep passing in the collection each time because the collection is actually a member of our class in this case now don't worry about the details the whole purpose here is just to illustrate the usefulness of using a class so you can see again that when we wanted to reverse the reverse just appeared when we typed call and the same for sort it's very easy to do so we'll just paste this one down here so that it will give us a header and we can see very clearly what the results look like so when we run the code you can see that we've got the results but the plea thing that we want to point out here is that our code is much neater and it's much easier to distribute this elsewhere so in the next example what we're going to look at is a user form now in this user form I wanted to create a filter like this so when you type in the letter it filters out all the data so you can easily find the data that you're looking for so this functionality it doesn't exist in VBA so I had to write the code to do this myself and when I write the code I put it in the user form now the big problem that we have with doing this is that every time I want to use their code I have to take all the code that you can see here on the screen and I have to put it into the user form and so this is quite a messy thing to do and quite a lot of work so rather than do this what I did was I take all this extra code and I place it into a class and then the user can just come along take a user form just add a textbox then add a ListView and then what we can do is just quite simply add the code and how we do it is we drag and drop our class so this is the class that has all the functionality that I want and in this class I have actually the cold included so you can just copy and paste this code that you need to put in your user form so we go to the user form we paste in the code and obviously we just commented out here and now that we have the code we basically just go to our code that calls the user form and then we just assign it to the range and this is the range where the data is so if this you just copied and paste one line of code and then we've added a line of code here just to assign the day that to whatever range the data is in and then when we run this we type in the name just any letter and you can see the functionality is already there and this is only possible because we used a class for the behavior and we can just easily drop the class into our project this video we covered class modules and why you need them in your code so well I hope this provided a clear understanding of what class modules are and that you now know even if you don't understand how to use them just yet but that you now know how they can benefit you in your code if any questions and queries please leave them in the comment section below thanks for watching this video I hope you found it useful if you would like to get notified when my new videos are published then please click on the subscribe button and then click on the bell icon beside it now if you'd like some more free excel vba resources then check out my website Excel macro mastery comm there are major articles on all the major areas of Excel VBA each article has an easy-to-navigate table of contents as well as a quick guide that allows you to easily find syntax you need and there's tons of coding examples that you can copy and use in your own macros you'll also find techniques that are not available anywhere else I also have a VBA tutorial and in this tutorial there's lots of activities and solutions so that you can try them all for yourself and it's all absolutely free so that's all for me and I hope to see you on my next video
Info
Channel: Excel Macro Mastery
Views: 56,717
Rating: 4.9142418 out of 5
Keywords: vba, excel, microsoft excel, excel vba, visual background, vba class modules, vba class module examples, vba class module tutorial, vba advanced tutorial, vba advanced excel, class modules in excel vba
Id: ie2Duci-qKQ
Channel Id: undefined
Length: 13min 23sec (803 seconds)
Published: Fri Oct 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.