Excel VBA: Using Class Modules with Collections (5/5)

  Рет қаралды 70,934

Excel Macro Mastery

Excel Macro Mastery

Күн бұрын

👉 Ready to master VBA?
- Check out my full courses: courses.excelm...
- Subscribe to the channel here: bit.ly/36hpTCY
- FREE CHEAT SHEET: Get the free cheat sheet on VBA arrays here: bit.ly/2MXsnz9
(Note: If the download page doesn't work then make sure to turn off any popup blockers)
In this Excel VBA video, we see how to use Class modules objects with Collections. One of the problems with collections is that it can only store one item from a row of data.
So how to we store data from multiple columns? We use class module objects.
In this video you will see how to create class module objects to store our data and how to add the modules to a collection. Finally we will see how to read the data from these class module objects to a worksheet.
Table of Contents:
00:42 Reading from a worksheet to a collection
03:11 Creating a Class Module
04:43 How to use the Class Module
07:15 Using the Class Object with a Collection
10:57 Write from the class objects to a worksheet
Related links:
The Ultimate Guide to Collections in Excel VBA: (excelmacromast...)
Get your free Arrays/Dictionary/Collections cheat sheet here: (bit.ly/2MXsnz9)
The Excel VBA Handbook Course(TheExcelVBAHan...)
Webinar Archives - 60+ Hours of VBA training(excelmacromast...)
Shortcut Keys:
Ctrl + R: View the Project Properties Window.
Ctrl + Shift + 8(or Ctrl + *): Get the current region on a worksheet.
F5: Run the code from the current sub.
F9(or click left margin): Add a breakpoint to pause the code.
Tab: To move lines of code to the right(Indent)
Shift + Tab: To move lines of code to the left(Outdent).

Пікірлер: 171
@rrrprogram8667
@rrrprogram8667 4 жыл бұрын
I went through the collection series over and over again.... And this is the best i have ever seen explained about the collection
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
Thanks. Glad you like it.
@vandalo7494
@vandalo7494 5 жыл бұрын
Absolutely awesome. I work with vba for a few years now and have been avoiding classes until now you made it so clear and simple to understand. Thank you very much. I will be looking for new videos.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Glad you like the explanation.
@turkaykoc8587
@turkaykoc8587 Жыл бұрын
I have watched all the 5 tutorials in series and I am very impressed about how good you did it. I am 59 years old, have some knowledge about VBA coding. Your tutorials are so compact, efficient and valuable. Thank you so much to you, living somewhere in the world. Sharing could be not better to me then your examples, thank you again, I appreciate your efforts.
@VBASqlAccessPowerApps
@VBASqlAccessPowerApps Жыл бұрын
true indeed
@rafab.4413
@rafab.4413 4 жыл бұрын
I've just discovered mixing collections with classes and it ma be the most powerful tool to make clear, easy to maintain code in large VBA applications. I am pretty sure you are using this a lot in your programs. One more (or maybe two..?) videos with practical implementing this concept would be incredibly helpful. E.g. for education purpose I am trying now to code something like relational database using Excel tables (a little like CRUD in SQL)- I am tired for of filtering, inserting, deleteting rows if condition is met, updating values all the time (copy-pasting macros all the time and adapting) in Excel tables. Just an idea. Thanks a lot, you're legend.
@stephenhammond1745
@stephenhammond1745 4 ай бұрын
I finally get it. The other examples I've seen of this were only storing 1 data item per row in the collection (e.g. Name) so it wasn't clear why using a class was a benefit. Now that I see you can store multiple data items it makes sense. I guess if you were working with multiple classes you could store that as dimensions in an array?
@wattjock2405
@wattjock2405 3 жыл бұрын
This was an outstanding tutorial for this beginner VBA Coder to learn about Class Modules. Thanks Paul!
@THNDRacket
@THNDRacket 3 жыл бұрын
Excellent series. I appreciated it very much.
@stevedavies5588
@stevedavies5588 2 жыл бұрын
Really enjoyed this little set of 5 videos. Very informative on how to use collections and classes.
@grayfoxfive
@grayfoxfive 5 жыл бұрын
Great example of a real-world use for classes in VBA. Looking forward to a series on class modules, methods and properties!
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
glad you like it. Classes are coming soon.
@onurtunc9468
@onurtunc9468 2 жыл бұрын
Class module was allien for me until now. You are perfect. i am looking for fast way to filter main data with criteria and pasta to another sheet. thanks again.
@syjo8105
@syjo8105 3 жыл бұрын
I've wanted know what the class modules are and why & when we use them. And the search brought me here. It's still vague and confusing to me but...... Your teaching videos are very helpful for me. And even though I'm not good at English(especially listening) it's relatively easy to understand. I think you are a excellent teacher. Two thumbs up! Thank you!
@wmcnabb
@wmcnabb 5 жыл бұрын
Awesome Paul. Very helpful and easy to follow.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Glad you like it Wyatt.
@khalidalisawi8037
@khalidalisawi8037 2 жыл бұрын
thanks for the videos. really it is interesting and I have learned a lot every day. I ask you to introduce an advanced video about classes
@Trucpq
@Trucpq 11 ай бұрын
it is a beautiful demo about Collection and Class Module. Thank you !
@mizukikaioh7986
@mizukikaioh7986 3 жыл бұрын
i love how you add animations, makes it even clearer ! thanks for sharing and helping out ;)
@TheZenytram
@TheZenytram 2 жыл бұрын
i was just using array for everything, this make all of what i was doing 100 times more flexible and easier to code holy shit, thank you. why i didnt thought of using collection with classes before, seems so obvious now.
@piotrlorenc1762
@piotrlorenc1762 4 жыл бұрын
Exactly what I was looking for, thank you very much, your tutorials are very easy to understand
@soberingtimes2362
@soberingtimes2362 5 жыл бұрын
Again, beautifully described Paul. I finally believe I might be able to get to grips with Excel VBA. Cheers
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Ronan.
@khalidalisawi8037
@khalidalisawi8037 2 жыл бұрын
Dear, Today I see your list about the collection. it was interesting and easy to understand
@jozsefolasz8702
@jozsefolasz8702 5 жыл бұрын
Thank you, extremely well chosen examples with extremely clear explanation.
@edgarsantarosa9847
@edgarsantarosa9847 3 жыл бұрын
Very helpful, it's kinda object oriented collection, much easily to handle and avoid further errors
@TasteDaRDX
@TasteDaRDX 5 жыл бұрын
Hi Paul, Excellent video, thanks for sharing and making so simple to understand.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
No problem. Glad you found it useful.
@vikramjagtap6849
@vikramjagtap6849 5 жыл бұрын
Excellent video. I am your fan
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Vikram.
@martinv5609
@martinv5609 5 жыл бұрын
Hi Paul, thanks for your detailed Tutorials. They are great! I will directly incorporate this in my macros.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
You're welcome.
@JayadevLenka
@JayadevLenka 2 жыл бұрын
Thanks Excellent !
@frikduplessis3869
@frikduplessis3869 5 жыл бұрын
Hi Paul, Thanks for brilliant videos and awesome explanations
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
You're welcome Frik.
@frikduplessis3869
@frikduplessis3869 5 жыл бұрын
Hi Paul, Please don't stop with this awesome short videos, I can't stop watching them, I have also tried out your techniques they are very helpful.👍
@TakahiroHanawa
@TakahiroHanawa 3 жыл бұрын
What great series of a collection usage. It’s very very useful technique to handle database in Excel! Thanks a lot!
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
You're very welcome!
@karnabudhathoki5311
@karnabudhathoki5311 4 жыл бұрын
Thanks Paul....Thank you very much ..... I have learned the high end concepts like Class,collection,dictionary and arrays from your videos..Thanks a lot........
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
You're very welcome!
@zsugo1
@zsugo1 2 жыл бұрын
Thank you, I struggled a lot with my MS Access application for a little mistake. Thankful your video I found and correct it.
@wayneedmondson1065
@wayneedmondson1065 5 жыл бұрын
Thanks Paul.. so in this case, the class module is like a collection inside a collection.. meaning the class module is a collection of data specific to a single record within a collection of records meeting specific criteria.. yes? Thanks for the intro to using class modules.. very helpful. Thanks for sharing your knowledge. Looking forward to more. Thumbs up!!
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Exactly Wayne. The collection is like the rows and then the class module contains the fields in that row.
@wayneedmondson1065
@wayneedmondson1065 5 жыл бұрын
@@Excelmacromastery Awesome.. thanks.. you are unlocking some things that I did not well understand prior. Thumbs up!
@christianbirkmann547
@christianbirkmann547 5 жыл бұрын
Thx. Very good Video! It helped me a lot
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
You're welcome
@GurgMaster
@GurgMaster 8 ай бұрын
Excellent video again. Great explanation and exactly the sort of task I have at hand. Only issue I had was the swimming pool effect on the mouse click was giving me a headache. (I'm pretty reactive to strobing and scrolling effects 😞) Just something you might want to note.
@strannostrannovasrr
@strannostrannovasrr 5 жыл бұрын
Great video Paul. The advanced filter is fast but does not provide any control. and that's the main disadvantage of this method. therefore I prefer the array method.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
That's a very interesting point Stranno. What do you mean "does not provide any control"?
@strannostrannovasrr
@strannostrannovasrr 5 жыл бұрын
Excel Macro Mastery Sorry. Wrong subject. My comment refers to your latest video about optimizing code with regard to increase the speed. if you want to change the the data on the fly in a particular column for instance, the advanced filter is not a suitable method.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
​@@strannostrannovasrr That's true. There are some situations where it is not suitable.
@krn14242
@krn14242 5 жыл бұрын
Thanks Paul. Great video. I found this information very very useful and look forward to incorporating this into my macros. Seems like it would be very fast with larger data sets.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
that's great you found it useful. Let me know how it works with your macros.
@krisstaniszewski7457
@krisstaniszewski7457 3 жыл бұрын
Paul, supper done. Thanks a lot. You are the best.
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
You are welcome!
@aduenamz2569
@aduenamz2569 3 жыл бұрын
Very good, thank you so much.
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
Thank you too!
@Smelch
@Smelch 2 жыл бұрын
Fantastic, very well explained . Thanks
@phuongphuongnetwork151
@phuongphuongnetwork151 5 жыл бұрын
Very nice video thanks for sharing 👍👍👍
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
you're very welcome.
@adammorait7429
@adammorait7429 3 жыл бұрын
Honestly, your channel is the best I found on KZbin about VBA. It is up to date and always useful for my projects. You should teach on Udemy.
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
Thanks, I actually do have a course on Udemy.
@adammorait7429
@adammorait7429 3 жыл бұрын
@@Excelmacromastery thanks I will take a look
@Meolimo
@Meolimo 5 жыл бұрын
awsome thks :-) From Montreal (Quebec)
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Mario
@juliocw
@juliocw 4 жыл бұрын
Very helpful and detailed explanation. Thank you!
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
You're very welcome!
@rrrprogram8667
@rrrprogram8667 5 жыл бұрын
Excellent stuff... Thanks for the videos
@PrincePedia
@PrincePedia 5 жыл бұрын
Great video,Thanks
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks for your support.
@pawel6170
@pawel6170 4 жыл бұрын
Thank you very much
@carlosguerrero7270
@carlosguerrero7270 4 жыл бұрын
Excellent video Sir, thank you very much, this is the best video I've ever seen. Cheers from Colombia
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
Glad you enjoyed it!
@johnbirtwistle2546
@johnbirtwistle2546 Жыл бұрын
Perhaps a video on best practice/examples when a class object is altered by a method and when not would be useful. A bit like ByRef and ByVal. Your explanations are excellent and code always clean. Thanks.
@PhuNguyen-gq5nh
@PhuNguyen-gq5nh 3 жыл бұрын
Thanks Paul, love you so much, thank for sharing your knowledge. I got what i want from lecture
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
cảm ơn Phu
@roshdialaqtam6981
@roshdialaqtam6981 4 жыл бұрын
If you are using class module just to store basic variables like in your example, you could use "Type" keyword as well .. thanks man for this great video
@MusicToMyEar1005
@MusicToMyEar1005 4 жыл бұрын
I have been thinking (for a long time!) if there is a way to manipulate like "VLOOKUP" in VBA, using "class" ideas. Because 1) VLOOKUP is a worksheet function, not VBA function. 2) I could emulate VLOOKUP by using "FIND" or something, but it gets messy. 3) I want to hold all the data in the range during the entire procedure. This is the answer!! THANK YOU VERY MUCH!!
@ventjemazzel8822
@ventjemazzel8822 4 жыл бұрын
Excellent video; thank you very much
@noviceprogrammer2011
@noviceprogrammer2011 4 жыл бұрын
Paul, Thanks for your video. One of the problems I find with learning class modules (and with it, collections), is why do we need it? In your video, I agree it's very clear to readers what is going on. However I feel (and do correct if I'm wrong!) that classes slow things down. Certainly in your video, using a collection to store the class, you had to loop at the end to return the results back onto Sheet1. This is how I usually do it (because I'm no class module expert)! Dim CountAus As Long CountAus = Application.WorksheetFunction.CountIf(Sheet1.Columns(3), "Australia") Dim DataArray() As Variant DataArray() = Sheet1.Cells(1, 1).CurrentRegion.Value Dim DataArrayRows As Long DataArrayRows = UBound(DataArray(), 1) Dim AusArray() As Variant ReDim AusArray(1 To CountAus, 1 To 2) As Variant Dim i As Long, j As Long j = 1 For i = 1 To DataArrayRows If DataArray(i, 3) = "Australia" Then AusArray(j, 1) = DataArray(i, 1) AusArray(j, 2) = DataArray(i, 4) j = j + 1 End If Next i Sheet1.Cells(1, 8).Resize(j - 1, 2).Value = AusArray() Can you please let me know your thoughts about the shortcomings of my code, other than it's not as clear as yours because mine does not include any headings? I would be grateful if you could post a video of a situation where ONLY a class module would do the trick. Thanks
@homejonny9326
@homejonny9326 4 жыл бұрын
I think there is not exist a problem that only class module would resolve. But using objects like these make life way more blissfull :)
@johndoge9003
@johndoge9003 2 жыл бұрын
Say your class has lots of variables, now say your class variables result in additional variables which you’ll need to iterate through to get the result you’re looking for. Having a hierarchy will make your code much more readable for anyone helping you, or fixing your code after you’ve moved up the latter.
@nathalischong1761
@nathalischong1761 5 жыл бұрын
This is good. Thank you for the sharing
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Chong
@excelpowerquerypowerbi586
@excelpowerquerypowerbi586 9 ай бұрын
Спасибо
@TravisFX
@TravisFX 3 жыл бұрын
Ahh! Just had the light bulb switch on! Great section Paul. So lemme get this straight... The first part to specify filter..i.e. Australia, easy enuf. Then use the class to fill collections. Then use the collection again to "fill the class" back for the write out.? I thought you were gonna write out what was in the collections directly. But I see what you did now. I can see doing this with just loops, ranges etc would take a lot more doing. Very cool.
@PanzerRanger
@PanzerRanger 5 жыл бұрын
This was a new way for me, this will save some time, Thanks!
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
You're welcome.
@fahadshaheen2373
@fahadshaheen2373 4 жыл бұрын
Really it baffles me why should you get a single thumb down on any of your videos. The way you explain your stuff is really impressive though I wish you go little bit slower. Thanks a ton
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
Thanks Fahad.
@peterlilley6819
@peterlilley6819 4 жыл бұрын
Nice explanation Paul. Is it possible to have the collection managed from within the class ? e.g. obviously the Collection must be declared (public) in the calling program. Then the Class (constructor, destructor) can simply call "coll.add customer" on each object instantiation. That way it only ever needs to be coded up once ?
@jimfitch
@jimfitch 2 жыл бұрын
I know it’s been 2 years since your comment/question. As I start exploring use of classes, I have the same question. Have you learned the answer to your question? If so, would you please share it?
@marcelh2341
@marcelh2341 5 жыл бұрын
I always used a temporary collection with all my information I want to collect (so for this example first and last name) and after getting this information I stored it in another collection (looks about the same in your video) with a key. Its close to a class module, not the same but also handy. At least in my opinion. Now I know another way. Thank you. :D
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks for the feedback Marcel.
@hadibq
@hadibq 2 жыл бұрын
Thanks,great video as always! I would also tested custom Type along with Collections, although I'm a big fan of class modules 🙂
@johndoge9003
@johndoge9003 2 жыл бұрын
Thank you sir. Do you have any recommendations if you want to “group by” a particular collection’s item?
@carcaracode1754
@carcaracode1754 4 жыл бұрын
tks very much
@ouzytheoriginal
@ouzytheoriginal 2 жыл бұрын
good described but it should have been in the playlist within classes thats my feedback to you
@accountsabsk
@accountsabsk 2 жыл бұрын
Nice Video. Thank you for explaining in very simple language. I tried to adopt this method to transfer filtered data to another sheet. Everything was fine, but numbers with decimals got rounded off in the destination sheet. In your video, if the Total Items of Joseph Velasquez were 40.65, it became 41. Kindly suggest me a solution. Thanks.
@VBASqlAccessPowerApps
@VBASqlAccessPowerApps Жыл бұрын
Collection is only 2 dimensional whereas array can be multi dimensional
@mariodinoia9586
@mariodinoia9586 3 жыл бұрын
Hi Paul I have learned quite a bit from this video it will streamline the code in my current projects I was just wondering for a simple example like this would be easier to use custom types instead of class modules and when would it be better not to
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
Custom types predate Classes. We normally use classes to make the code follow OOP methods. That said, I haven't compared them speed wise so it's possible there is a speed advantage.
@MrMallesh1
@MrMallesh1 5 жыл бұрын
Thanks Paul , Great Video, can you make one more video on get and let method in class module, and their advantage if any Thanks.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Mallesh. I plan to cover Class Modules in a series of videos. In the meantime, you can check out this article which cover the get and let properties: excelmacromastery.com/vba-class-modules/#Class_Module_Properties
@SmilingGriffin
@SmilingGriffin 5 жыл бұрын
Many Thx for such an excellent tutorial. Would there be chance on how to incorporate COLLECTION, CLASS and ADO (ACE.OLEDB.12.0) together?
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks. You don't really use them together. ADO reads the data and places it in a recordset which is then written to the worksheet. Collections and Classes are used as interim places to store data. Between reading and writing.
@dildokafir5058
@dildokafir5058 5 жыл бұрын
Hello kelly after this informative upload , on which subject of vba you gonna make tutorial.??
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
The Dictionary is up next😀
@ralaa82
@ralaa82 4 жыл бұрын
Thanks for the video .its very helpful. Can we store them into array instead of collection .and what is the cons and pros of using either. Appreciate your answer mate.
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
Yes you can
@ralaa82
@ralaa82 4 жыл бұрын
Thanks bro.. actually i went back to the previous series and realise that you did actually explained that.. Keep it up ..you are a legend
@sureshkusumaa
@sureshkusumaa 3 жыл бұрын
Very well explained. I have a query: could you please explain code to create a new excel workbook and write the data to the new workbook??. Thanks in advance👍👍
@franceschiguillaume5870
@franceschiguillaume5870 Жыл бұрын
Hi Paul, a big thanks for this set of insightful videos. I've got a question though and I hope it's not off-base regarding what you taught us in this last tuto. Is it possible to create collections in a bigger collection (like if I were to create one main collection and then, multiple sub collections that would be stored in the first main one) ? If I explain my project a little bit more : I work within a digital team for a Railway company, so the data I have to handle is a list of trips that each have a specific (and variable) list of points (stations). So I'd want to create a first collection that'll allow me to store all my trips objects. And then I'd want to create one collection of "stations" for each one of my trip. Could that be possible ? I don't know how to start : - Should I create a class object named "trip" and then put a collection named "stationList" as one of the variables of my object trip ? --> But I can't find a way to handle a collection as a variable of a class Object that will be also stored into another collection itself Thanks in advance for your help Kind regards, I look forward to listening to another video of yours
@GersonCarhuapoma
@GersonCarhuapoma 5 жыл бұрын
Paul what is the difference between locals window and watch window? And which do you recommend to follow colls/arrays/dicts? Thnk u very much
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
locals automatically adds variables that are active. You cannot add variables or edit the locals window. The watch window allows you to add variables and edit them. You can add function calls, formulas and be very creative. Personally i only use the watch window as the locals window is limitedq.
@GersonCarhuapoma
@GersonCarhuapoma 5 жыл бұрын
@@Excelmacromastery Thanks Paul. Also i found Locals w doesn't follow dict variables as objects. Now I'm using only Watch w. Thnk u very very much.
@Alexseehp
@Alexseehp 3 жыл бұрын
Nice video. Just to check. Is it possible to autopopulate the variables in the class?
@dangelorrrr
@dangelorrrr 5 жыл бұрын
Hello Paul, Is there a way to access the four different values in the collection item directly? Something like coll(i,).item (3) for the "Country"?
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
you can use Coll (i).Country. Coll (i) returns the class module object.
@halhirst2624
@halhirst2624 Жыл бұрын
Well, I have had to learn a bit of VBA the "Hard Way', ... just watching some of your material has helped immensely.. When digging around for information, (when you have no background in this stuff) is a little overbearing to say the least. But with help from different ends it is starting to make sense. I'm to the point now where writing little macros isn't cutting it, and watching your explanation on the layout of Subs, Classes Modules etc has me looking at things totally different. Its not completely understood,.... but I'm heading in the right direction. Thanks.
@VBASqlAccessPowerApps
@VBASqlAccessPowerApps Жыл бұрын
true
@gerdamft9099
@gerdamft9099 2 жыл бұрын
Paul what if I wanted to add another worksheet of data to make the collection like a 3 dimensional array? How would that be done?
@p.n.unnikrishnan6659
@p.n.unnikrishnan6659 4 жыл бұрын
Very nice teaching. Thanks for ur efforts. Can u also give us a multi level (7 combo box) with depended unique data. I tried but getting only up 3 level. Next shows error. Thanks
@salvatoredelprete6167
@salvatoredelprete6167 4 жыл бұрын
Quick question: In this example we have set the condition "Australia" to create the collection. Let's say that I want to create a collection for each country, but I want VBA to recognise the number and name of the counties form the range, how should we set the condition? Thanks!
@m-squaredcontractors9720
@m-squaredcontractors9720 3 жыл бұрын
Hi Paul. Thank you for the great tutorial. What is the whole point of "CustomerOut" at the end? I've tried it out without it and it works. I just used the original "Customer"
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
To avoid confusion. In this situation it will work but if the code gets more complex it is better not to have the same variable doing two different jobs.
@EdgarRoock
@EdgarRoock 3 жыл бұрын
Question: Given that Surname is a unique identifier, how can I refer to the item with Surname "Benton" without having to loop through the entire collection?
@EdgarRoock
@EdgarRoock 3 жыл бұрын
Solved it, the line to add the class object has to change to: coll.add customer, customer.Surname This will add Surname as the key.
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
Glad you got it sorted.
@limasc001
@limasc001 3 жыл бұрын
Only a question, can we do something like this with UDT.?
@JayJay-fl1hc
@JayJay-fl1hc 4 жыл бұрын
I have a little question: my teacher gave me a code with variables and i should put the variables in a classmodule and the button should work the same way like it did before. Is it the same way like you did in the video?
@fvh500
@fvh500 3 жыл бұрын
Does the clsCustomer has to be a Public Data Class or can it be Private Data Class and then Public Property Class, or would the latter interfere with the Add to Collection?
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
You can use properties if you prefer. This is the proper way to do OO code. However I have never found any benefit to using properties for classes that only hold data. It adds an extra abstraction for no benefit.
@shantela2282
@shantela2282 3 жыл бұрын
Nice! I'm trying to copy the data from 46 worksheets and combine them into one. I used another coding before, but it capped out at 25 worksheets. I was told to try to complete the task using a collections. The problem is, I'm not sure how to construct the coding. Is this something you can assist with?
@aNDy-qh1em
@aNDy-qh1em 3 жыл бұрын
Hello, you can do it with the help of collections, but i guess it would work faster with arrays. Still, if you prefer collections, first create class module for your data type. First you create collection, then first loop goes through all source sheets, secondly you loop data inside your sheet row by row , creating instance of your class, filling it with data from row and finally adding it to the collection. Then when you have filled your collection, loop through it filling out the target sheet.
@manuelgeoffrey9188
@manuelgeoffrey9188 3 жыл бұрын
Dear Sir, thank you for sharing. I'm retrieving response text using html inside a class module. Given a column of web page links to web scrape, can we input the response text (in this case stock prices) immediately to a range of cells without using collection ? At the moment, my code only retrieve the stock prices on C2, so only cell C2 get updated over and over again with different prices. Kindly need your help.
@drancerd
@drancerd 3 жыл бұрын
You say that= For Each it's faster than just For. So i f i use => For Each costumer in coll (then paste all into a sheet with an i=i+1) still be faster?
@themannamedjames8766
@themannamedjames8766 4 жыл бұрын
What would be the advantage of this over using an advanced filter?
@edsonmatheus7976
@edsonmatheus7976 4 жыл бұрын
I think that was just for learning purpose. I'd use an array.
@shep7484
@shep7484 4 жыл бұрын
at 5:25 in the UseCustomers sub, after you declare and set the variable O to clsCustomer, you type o. and intellisense displays the headings defined in clsCustomer. When I type o. intellisense doesn't seem to be working. What might I be doing wrong. (PS I am loving your series of KZbin contributions on VBA.)
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
Of Intellisense isn't working it's normally because there is an error in your code or a variable isn't declared correctly. Use debug.compile to remove errors and try again.
@shep7484
@shep7484 4 жыл бұрын
@@Excelmacromastery You hit the nail on the head. After your quick and timely response, I noticed that I left public off of two of the declarations. correcting that solved the propblem. Thanks so much for being willing to help out an older newbie.
@glennlee5851
@glennlee5851 5 жыл бұрын
Great video! subscribed. Thanks! Could do some pivot table and pivot chart programming videos when you get a chance ?
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Thanks Glenn.
@yeahnick4260
@yeahnick4260 Жыл бұрын
There 7:22 i understood, why we do create every time a new object "set o = new clsCustomer" but why doenst it work if we just insert new values into our already existing o object? I tried it but it doenst work and i dont unterstand why
@vuminhduc2011
@vuminhduc2011 4 жыл бұрын
Can we use user defined data types instead of Class module?
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
You can but it's considered better practice to use Class Modules.
@olfertrichter6738
@olfertrichter6738 5 жыл бұрын
Hi Paul Why use collection. Array do just the same.
@Excelmacromastery
@Excelmacromastery 5 жыл бұрын
Hi Ole. They are quite different as you can see in video on collection vs arrays in the collections playlist. For example, an array requires code to resize but a collection does it automatically.
@noviceprogrammer2011
@noviceprogrammer2011 4 жыл бұрын
I agree, look at my comment.
@confidential303
@confidential303 4 жыл бұрын
I want to write a function where I have multipe columns with undefined number of values, but I don't know it beforehand since it will change per scenario. For instance column A Column B ... Column Z 1 2 4 2 4 8 I want to scan the columns and then Add them into an Array or Collection, but problem is I don't know beforehand how much arrays I need , in this case it goes from A to Z , so I need then 26 arrays . So I have to create in the program 26 arrays how to do that or collections. i tried in a for loop dim array&i() as array but I get error message. Or do you need to make an array of an array, where array(1) points to ArrayA. Hope somebody can help.
@noviceprogrammer2011
@noviceprogrammer2011 4 жыл бұрын
Paul, Normally when you declare a class or collection, you write: Dim Coll As Class / Collection This is normally followed by Set Coll = New Class / Collection In your video, (towards the end when you write out the data to column H), you put: Dim CustomerOut As ClsCustomer and a few lines later, you wrote: Set CustomerOut = Coll(I) I expected to see Set CustomerOut = New ClsCustomer So my question is: when do you NOT have to follow up: Dim SomeVariable As SomeClass with Set somevariable = New SomeClass? Also instead of using a class to write out the data, you could have written Sheet1.Cells(i, "H").Value = Coll(I).FirstName Sheet1.Cells(I,"I").Value = Coll(I).TotalItems
@rolfdoets
@rolfdoets 4 жыл бұрын
Thank you for your top video', very clear explained. I have a question? Is it possible to sum or substract collections? For example: you have collection A = {1,2,3} and collection B = {2,3,5) So, A-B is then {1} or A intersection B is {2,3} or A union B = {1,2,3,5} I wonder if VBA can do this? ..... maybe an idea for you next video?.... Tnx again
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
No. It can't do anything like this.
@confidential303
@confidential303 4 жыл бұрын
You have to write your own function for it.
@mariodinoia9586
@mariodinoia9586 3 жыл бұрын
Hi Paul This is great it will make my coding life much easier Just wondering for a simple example like this Would it be better to use custom types and when would it be best not to
@Excelmacromastery
@Excelmacromastery 3 жыл бұрын
Normally we use classes when we are storing multipe pieces of data about something. For example a company, sales records, persons details.
@naumansarwar8216
@naumansarwar8216 4 жыл бұрын
i tried using the collection function below which is useful for me as currently i am using a long array formula to get the result but when i run the below with different module it give me a correct result but when i run it in a cell as =abc(2) it gives #Value error Function abc(x As Variant) Dim coll As New Collection Dim Rg As Range Set Rg = Worksheets("Sheet1").Range("A1").CurrentRegion Dim i For i = 1 To Rg.Rows.Count If Rg.Cells(i, 1).Value = "Sales" Then coll.Add Rg.Cells(i, 2).Value End If Next i abc = coll(x) End Function
@VillaOuk
@VillaOuk 4 жыл бұрын
Thank you very much, Paul. It's very much useful. I watch most of videos and learnt a lot from those. In addition, Could you share codes that can send messages from Excel VBA to WhatsApp number or group. Thank you
@wattjock2405
@wattjock2405 3 жыл бұрын
Very ignorant question here... why use "i" as a variable for row in the For Next Loop rather than use "r" as the Variable for Row?
@houstonvanhoy7767
@houstonvanhoy7767 3 жыл бұрын
Watt: It's not a rule. But it is a decades-old practice in numerous programming languages when defining and using loops. The only ignorant question is the question that you didn't ask.
@andrevanroy3099
@andrevanroy3099 6 ай бұрын
What is it with you and Jenny Jones
@noviceprogrammer2011
@noviceprogrammer2011 4 жыл бұрын
When writing out the results, instead of declaring a new class as per the video: Dim customerOut As clsCustomer For i = 1 to coll.Count Set customerOut = Coll(I) Sheet.cells(I, "H").Value = customerOut.Firstname Why not just write: For i = 1 to coll.Count Sheet.cells(I, "H").Value = Coll(I).Firstname
@Excelmacromastery
@Excelmacromastery 4 жыл бұрын
To make the code clearer for less advanced users.
@noviceprogrammer2011
@noviceprogrammer2011 4 жыл бұрын
@@ExcelmacromasteryThanks
How to Use Class Interfaces in Excel VBA
20:16
Excel Macro Mastery
Рет қаралды 80 М.
Excel VBA Collections: Collections vs Arrays (4/5)
15:14
Excel Macro Mastery
Рет қаралды 57 М.
Фейковый воришка 😂
00:51
КАРЕНА МАКАРЕНА
Рет қаралды 7 МЛН
这三姐弟太会藏了!#小丑#天使#路飞#家庭#搞笑
00:24
家庭搞笑日记
Рет қаралды 125 МЛН
How to make your Excel VBA code run 1000 times faster
16:55
Excel Macro Mastery
Рет қаралды 360 М.
How to get the Last Row in VBA(The Right Way!)
15:41
Excel Macro Mastery
Рет қаралды 159 М.
10 Years of VBA Array Knowledge in 40 Mins
41:57
Excel Macro Mastery
Рет қаралды 19 М.
5 Things I wish I knew When I started using Excel VBA
12:45
Excel Macro Mastery
Рет қаралды 337 М.
5 More things I wish I Knew When I Started Using Excel VBA
13:31
Excel Macro Mastery
Рет қаралды 71 М.
Watch these 28 minutes if you want to become an Advanced VBA user...
29:01
Excel Macro Mastery
Рет қаралды 53 М.