VBA Classes
HTML-код
- Опубликовано: 22 июл 2024
- In this video I explain about classes. Things that we cover is
What are classes?
Why do we need classes?
What are properties, methods and events w.r.t classes
and so many other things
This video will be my building block for my next video which will explain about Object Oriented Programming in VBA so if you are not aware what classes are and how they work then I recommend watching this video.
----
00:00 Intro
04:54 Properties
05:52 Get, Set and Let
08:22 Creating an Object of a Class
12:33 Methods
29:16 Events
39:26 Outro
----
Practice Workbook Download: www.dropbox.com/s/24v25niaota...
Software used for recording and editing this video
Camtasia: techsmith.z6rjha.net/SidRecom...
Disclaimer: The above is an affiliate link. At no additional cost to you, we will receive a small commission if you make a purchase using the above links. This helps support the channel. Thank you in advance for your support!
Connect with me on social:
LinkedIn: / siddharthrout
Facebook: / sidzrout
Instagram: / sidzthekidz
Stackoverflow: stackoverflow.com/users/11405...
Microsoft Excel VBA Facebook Group: / msexcelvba
#ExcelVBA #ExcelMacro #VBAExcel #MSExcel #Excel
Holy moly! I’ve been scripting with VBA for years but struggled with understanding classes. This is the video I needed. This new knowledge will help me right away in a project I am working on. Thank you!
Thank you so much for your kind words! 🌟 I'm thrilled to hear that the video helped you understand VBA classes better. It's amazing to see how learning something new can immediately impact your projects. If you have any more questions or need further clarification as you dive into your current project, feel free to reach out. Keep up the fantastic work, and happy coding!
EXCELLENT Tutorial!!!!!!...... using real world example makes it so much easier to understand! - a couple of small requests/suggestions. Could you step through the code and explain as the program runs - it helps to understand the process flow. As you advance set breakpoints so there is not a lot of repetition. A second request/suggestion is to show how to crerate a user interface - perhaps as a follow up video? - A REALLY great job!!!...
Thanks for the suggestions. Will try and incorporate them in my next video :)
Amazing job!
Thanks for taking the time out to appreciate it. Strange, I did not get an alert for your comment.
Loved the way you explained. Thank you!! 😄
Keep it up!
Glad you liked it Ramandeep :)
Thanks for the explanation mr. Sidd. OOP is great if you want to be a good developer (i think). I've read a lot of concepts from internet, but there are misunderstanding data. This way is great. 🤗🤗
Thanks Jiri for taking the time out to appreciate it 😊
Great, clear, instructive video on classes using vba. Added it to my references of recommended sample code.
Thanks for taking the time out to appreciate it.
Excellent video Sid - very instructive!
Glad you like it. And thank you for taking the time out to appreciate it. :)
Hi Siddharth, this is my first time on your channel and I hit the subscribe button after only two minutes. Your easy to understand explanation at the beginning of the video has already convinced me. Well done! Keep up the good work!
Thanks for taking the time out to appreciate it.
Хоть и не на русском но я все равно понял основу принципа работы. Самый хороший обзор. Большое спасибо брат! Успехов твоему каналу. 🤝
Рад помочь. Если у вас есть вопросы, вы можете задать их на русском языке, и я постараюсь вам ответить :)
Why do you use composition?
You could have declared a variable in the standard module, such as Dim MyVariable As Gear.
Then in Sub DriveCar, you can replace .TheGear.GearUp with MyVariable.GearUp.
Similarly for .TheGear.GearDown with MyVariable.GearDown.
How does using TheCar.TheGear.GearUp help? I think it confuses people (certianly me)!
Thanks
Very good question! I used composition so that the relationship can be maintained. This way if the parent object is deleted, then the child object also loses its status. For example, if the car is destroyed then the Gear is also destroyed. If I keep it in a module separately then there is no relationship. Even when the car object is destroyed, the gear object will still be available. You don't want the gear to be dangling around after the car is destroyed. 😊
I created classes just so I can move multiple variables via a dictionary. Should I instead move the code that uses the variables into the class methods and just instantiate the class every time I need to manipulate data associated with the class?
While I can't comment without looking at your code, it is a good idea in general to move variables inside classes and expose them via properties. This is in accordance with OOP concept and is called "encapsulation". It makes for better code overall.
I couldn't understand how event Works in class Module, you should have created this video in multiple parts in details by telling each and every concepts line by line.. 😂
I want to learn WithEvent and Raise Event concept in details, so make a Video on these topics as well.
The code in the downloaded workbook is different to the video.
Sub MyCarExperience()
CreateNewCar
FillFuel
DriveTheCar
Dim CarTyreState As Double
CarTyreState = TheCar.TheTyre.TyreCondition
If CarTyreState < 40 Then
MsgBox "Car Tyre Condition: " & CarTyreState & "." _
& vbNewLine & "We will replace tyres now"
Set TheCar.TheTyre = New Tyre
MsgBox "Car tyres have been replaced."
MsgBox "Car Tyre Condition: " & CarTyreState & "."
End If
Set TheCar = Nothing
End Sub
This line is wrong in the download:
MsgBox "Car Tyre Condition: " & CarTyreState & "."
but it's correct in the video.
Thank you for bringing it to my notice. Will check and rectify it within 24 hours :)
Fixed! Thanks again.