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

Sdílet
Vložit
  • čas přidán 5. 09. 2024
  • 👉 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).

Komentáře • 171

  • @rrrprogram8667
    @rrrprogram8667 Před 4 lety +3

    I went through the collection series over and over again.... And this is the best i have ever seen explained about the collection

  • @turkaykoc8587
    @turkaykoc8587 Před rokem

    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.

  • @stephenhammond1745
    @stephenhammond1745 Před 3 měsíci

    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?

  • @vandalo7494
    @vandalo7494 Před 4 lety +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.

  • @rafab.4413
    @rafab.4413 Před 4 lety +6

    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.

  • @onurtunc9468
    @onurtunc9468 Před 2 lety

    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.

  • @wattjock2405
    @wattjock2405 Před 3 lety

    This was an outstanding tutorial for this beginner VBA Coder to learn about Class Modules.
    Thanks Paul!

  • @syjo8105
    @syjo8105 Před 3 lety

    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!

  • @TheZenytram
    @TheZenytram Před 2 lety

    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.

  • @grayfoxfive
    @grayfoxfive Před 4 lety

    Great example of a real-world use for classes in VBA. Looking forward to a series on class modules, methods and properties!

  • @THNDRacket
    @THNDRacket Před 3 lety +1

    Excellent series. I appreciated it very much.

  • @stevedavies5588
    @stevedavies5588 Před 2 lety

    Really enjoyed this little set of 5 videos. Very informative on how to use collections and classes.

  • @mizukikaioh7986
    @mizukikaioh7986 Před 3 lety

    i love how you add animations, makes it even clearer ! thanks for sharing and helping out ;)

  • @strannostrannovasrr
    @strannostrannovasrr Před 5 lety +1

    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  Před 5 lety

      That's a very interesting point Stranno. What do you mean "does not provide any control"?

    • @strannostrannovasrr
      @strannostrannovasrr Před 5 lety

      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  Před 5 lety

      ​@@strannostrannovasrr That's true. There are some situations where it is not suitable.

  • @edgarsantarosa9847
    @edgarsantarosa9847 Před 3 lety

    Very helpful, it's kinda object oriented collection, much easily to handle and avoid further errors

  • @vikramjagtap6849
    @vikramjagtap6849 Před 5 lety +1

    Excellent video. I am your fan

  • @wmcnabb
    @wmcnabb Před 5 lety +1

    Awesome Paul. Very helpful and easy to follow.

  • @zsugo1
    @zsugo1 Před 2 lety

    Thank you, I struggled a lot with my MS Access application for a little mistake. Thankful your video I found and correct it.

  • @Trucpq
    @Trucpq Před 11 měsíci

    it is a beautiful demo about Collection and Class Module. Thank you !

  • @soberingtimes2362
    @soberingtimes2362 Před 5 lety +1

    Again, beautifully described Paul. I finally believe I might be able to get to grips with Excel VBA. Cheers

  • @GurgMaster
    @GurgMaster Před 8 měsíci

    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.

  • @wayneedmondson1065
    @wayneedmondson1065 Před 5 lety +6

    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  Před 5 lety +4

      Exactly Wayne. The collection is like the rows and then the class module contains the fields in that row.

    • @wayneedmondson1065
      @wayneedmondson1065 Před 5 lety +3

      @@Excelmacromastery Awesome.. thanks.. you are unlocking some things that I did not well understand prior. Thumbs up!

  • @piotrlorenc1762
    @piotrlorenc1762 Před 4 lety +1

    Exactly what I was looking for, thank you very much, your tutorials are very easy to understand

  • @johnbirtwistle2546
    @johnbirtwistle2546 Před rokem

    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.

  • @jozsefolasz8702
    @jozsefolasz8702 Před 5 lety +1

    Thank you, extremely well chosen examples with extremely clear explanation.

  • @adammorait7429
    @adammorait7429 Před 3 lety

    Honestly, your channel is the best I found on CZcams about VBA. It is up to date and always useful for my projects. You should teach on Udemy.

  • @MusicToMyEar1005
    @MusicToMyEar1005 Před 4 lety

    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!!

  • @karnabudhathoki5311
    @karnabudhathoki5311 Před 4 lety

    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........

  • @christianbirkmann547
    @christianbirkmann547 Před 5 lety +1

    Thx. Very good Video! It helped me a lot

  • @khalidalisawi8037
    @khalidalisawi8037 Před 2 lety

    Dear, Today I see your list about the collection. it was interesting and easy to understand

  • @martinv5609
    @martinv5609 Před 5 lety +1

    Hi Paul, thanks for your detailed Tutorials. They are great! I will directly incorporate this in my macros.

  • @TasteDaRDX
    @TasteDaRDX Před 5 lety +1

    Hi Paul,
    Excellent video, thanks for sharing and making so simple to understand.

  • @roshdialaqtam6981
    @roshdialaqtam6981 Před 4 lety

    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

  • @krn14242
    @krn14242 Před 5 lety +2

    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  Před 5 lety

      that's great you found it useful. Let me know how it works with your macros.

  • @khalidalisawi8037
    @khalidalisawi8037 Před 2 lety

    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

  • @frikduplessis3869
    @frikduplessis3869 Před 5 lety +1

    Hi Paul,
    Thanks for brilliant videos and awesome explanations

    • @Excelmacromastery
      @Excelmacromastery  Před 5 lety +2

      You're welcome Frik.

    • @frikduplessis3869
      @frikduplessis3869 Před 5 lety +1

      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.👍

  • @JayadevLenka
    @JayadevLenka Před 2 lety

    Thanks Excellent !

  • @fahadshaheen2373
    @fahadshaheen2373 Před 4 lety +2

    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

  • @TravisFX
    @TravisFX Před 3 lety

    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.

  • @TakahiroHanawa
    @TakahiroHanawa Před 3 lety

    What great series of a collection usage. It’s very very useful technique to handle database in Excel! Thanks a lot!

  • @Meolimo
    @Meolimo Před 5 lety +1

    awsome thks :-)
    From Montreal (Quebec)

  • @PrincePedia
    @PrincePedia Před 5 lety +1

    Great video,Thanks

  • @noviceprogrammer2011
    @noviceprogrammer2011 Před 4 lety +1

    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 Před 4 lety +1

      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 Před 2 lety

      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.

  • @rrrprogram8667
    @rrrprogram8667 Před 5 lety +1

    Excellent stuff... Thanks for the videos

  • @Smelch
    @Smelch Před 2 lety

    Fantastic, very well explained . Thanks

  • @phuongphuongnetwork151
    @phuongphuongnetwork151 Před 5 lety +1

    Very nice video thanks for sharing 👍👍👍

  • @aduenamz2569
    @aduenamz2569 Před 3 lety

    Very good, thank you so much.

  • @pawel6170
    @pawel6170 Před 4 lety

    Thank you very much

  • @krisstaniszewski7457
    @krisstaniszewski7457 Před 3 lety

    Paul, supper done. Thanks a lot. You are the best.

  • @carlosguerrero7270
    @carlosguerrero7270 Před 4 lety

    Excellent video Sir, thank you very much, this is the best video I've ever seen. Cheers from Colombia

  • @juliocw
    @juliocw Před 4 lety

    Very helpful and detailed explanation. Thank you!

  • @PhuNguyen-gq5nh
    @PhuNguyen-gq5nh Před 3 lety

    Thanks Paul, love you so much, thank for sharing your knowledge. I got what i want from lecture

  • @marcelh2341
    @marcelh2341 Před 5 lety +1

    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

  • @ventjemazzel8822
    @ventjemazzel8822 Před 4 lety

    Excellent video; thank you very much

  • @peterlilley6819
    @peterlilley6819 Před 4 lety +1

    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 Před 2 lety

      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?

  • @hadibq
    @hadibq Před 2 lety

    Thanks,great video as always!
    I would also tested custom Type along with Collections, although I'm a big fan of class modules 🙂

  • @nathalischong1761
    @nathalischong1761 Před 5 lety +1

    This is good. Thank you for the sharing

  • @PanzerRanger
    @PanzerRanger Před 5 lety

    This was a new way for me, this will save some time, Thanks!

  • @excelpowerquerypowerbi586
    @excelpowerquerypowerbi586 Před 9 měsíci

    Спасибо

  • @ouzytheoriginal
    @ouzytheoriginal Před 2 lety

    good described but it should have been in the playlist within classes thats my feedback to you

  • @carcaracode1754
    @carcaracode1754 Před 4 lety

    tks very much

  • @johndoge9003
    @johndoge9003 Před 2 lety +1

    Thank you sir. Do you have any recommendations if you want to “group by” a particular collection’s item?

  • @MrMallesh1
    @MrMallesh1 Před 5 lety +1

    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  Před 5 lety +1

      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

  • @accountsabsk
    @accountsabsk Před 2 lety +1

    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.

  • @mariodinoia9586
    @mariodinoia9586 Před 3 lety

    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  Před 3 lety +1

      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.

  • @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

  • @sureshkusumaa
    @sureshkusumaa Před 3 lety

    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👍👍

  • @SmilingGriffin
    @SmilingGriffin Před 5 lety +1

    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  Před 5 lety

      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.

  • @halhirst2624
    @halhirst2624 Před rokem

    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.

  • @ralaa82
    @ralaa82 Před 4 lety

    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  Před 4 lety

      Yes you can

    • @ralaa82
      @ralaa82 Před 4 lety

      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

  • @dildokafir5058
    @dildokafir5058 Před 5 lety +1

    Hello kelly after this informative upload , on which subject of vba you gonna make tutorial.??

  • @Alexseehp
    @Alexseehp Před 3 lety

    Nice video. Just to check. Is it possible to autopopulate the variables in the class?

  • @p.n.unnikrishnan6659
    @p.n.unnikrishnan6659 Před 4 lety

    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

  • @gerdamft9099
    @gerdamft9099 Před 2 lety

    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?

  • @GersonCarhuapoma
    @GersonCarhuapoma Před 5 lety +1

    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  Před 5 lety +2

      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 Před 5 lety

      @@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.

  • @limasc001
    @limasc001 Před 3 lety

    Only a question, can we do something like this with UDT.?

  • @glennlee5851
    @glennlee5851 Před 4 lety

    Great video! subscribed. Thanks! Could do some pivot table and pivot chart programming videos when you get a chance ?

  • @dangelorrrr
    @dangelorrrr Před 5 lety +1

    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  Před 5 lety +2

      you can use Coll (i).Country.
      Coll (i) returns the class module object.

  • @m-squaredcontractors9720

    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  Před 3 lety

      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.

  • @salvatoredelprete6167
    @salvatoredelprete6167 Před 4 lety

    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!

  • @JayJay-fl1hc
    @JayJay-fl1hc Před 4 lety

    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?

  • @yeahnick4260
    @yeahnick4260 Před rokem

    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

  • @drancerd
    @drancerd Před 3 lety

    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?

  • @manuelgeoffrey9188
    @manuelgeoffrey9188 Před 3 lety

    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.

  • @themannamedjames8766
    @themannamedjames8766 Před 4 lety

    What would be the advantage of this over using an advanced filter?

    • @edsonmatheus7976
      @edsonmatheus7976 Před 4 lety

      I think that was just for learning purpose. I'd use an array.

  • @shantela2282
    @shantela2282 Před 3 lety

    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 Před 3 lety

      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.

  • @fvh500
    @fvh500 Před 3 lety

    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  Před 3 lety +1

      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.

  • @noviceprogrammer2011
    @noviceprogrammer2011 Před 4 lety

    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

  • @confidential303
    @confidential303 Před 4 lety

    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.

  • @shep7484
    @shep7484 Před 4 lety

    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 CZcams contributions on VBA.)

    • @Excelmacromastery
      @Excelmacromastery  Před 4 lety

      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 Před 4 lety

      @@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.

  • @naumansarwar8216
    @naumansarwar8216 Před 4 lety

    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

  • @EdgarRoock
    @EdgarRoock Před 3 lety

    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 Před 3 lety

      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  Před 3 lety

      Glad you got it sorted.

  • @olfertrichter6738
    @olfertrichter6738 Před 5 lety +1

    Hi Paul Why use collection. Array do just the same.

    • @Excelmacromastery
      @Excelmacromastery  Před 5 lety

      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 Před 4 lety

      I agree, look at my comment.

  • @vuminhduc2011
    @vuminhduc2011 Před 4 lety

    Can we use user defined data types
    instead of Class module?

    • @Excelmacromastery
      @Excelmacromastery  Před 4 lety

      You can but it's considered better practice to use Class Modules.

  • @mariodinoia9586
    @mariodinoia9586 Před 3 lety

    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  Před 3 lety

      Normally we use classes when we are storing multipe pieces of data about something. For example a company, sales records, persons details.

  • @rolfdoets
    @rolfdoets Před 4 lety

    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

  • @VillaOuk
    @VillaOuk Před 4 lety

    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

  • @andrevanroy3099
    @andrevanroy3099 Před 6 měsíci

    What is it with you and Jenny Jones

  • @wattjock2405
    @wattjock2405 Před 3 lety

    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 Před 3 lety +1

      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.

  • @noviceprogrammer2011
    @noviceprogrammer2011 Před 4 lety +1

    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