Use this Proven Method to Create Any Class Module

Поделиться
HTML-код
  • Опубликовано: 22 дек 2021
  • 👉 Ready to master VBA?
    - Check out my full courses: courses.excelmacromastery.com/
    - Subscribe to the channel here: bit.ly/36hpTCY
    -Want to download the source code for this video? Go here: bit.ly/3J8Zzyf
    (Note: If the download page doesn't work then make sure to turn off any popup blockers)
    Create any Class Module Using This Simple Method
    In this video, I'm going to show you a simple method to create any Class Module. This will save you a lot of wasted time and frustration as it will help you create the class you want.
    #ClassModule #VBAClassModule
    Useful VBA Shortcut Keys
    ========================
    Debugging:
    Compile the code: Alt + D + L OR Alt + D + Enter
    Run the code from the current sub: F5
    Step into the code line by line: F8
    Add a breakpoint to pause the code: F9(or click left margin)
    Windows:
    View the Immediate Window: Ctrl + G
    View the Watch Window: Alt + V + H
    View the Properties Window: F4
    Switch between Excel and the VBA Editor: Alt + F11
    View the Project Explorer Window: Ctrl + R
    Writing Code:
    Search keyword under cursor: Ctrl + F3
    Search the word last searched for: F3
    Auto complete word: Ctrl + Space
    Get the definition of the item under the cursor: Shift + F2
    Go to the last cursor position: Ctrl + Shift + F2
    Get the current region on a worksheet: Ctrl + Shift + 8(or Ctrl + *)
    To move lines of code to the right(Indent): Tab
    To move lines of code to the left(Outdent): Shift + Tab
    Delete a Line: Ctrl + Y(note: this clears the clipboard)
  • НаукаНаука

Комментарии • 46

  • @Excelmacromastery
    @Excelmacromastery  2 года назад +10

    I hope you enjoy the video. Let me in the comments if this method is something you will use...

  • @wayneedmondson1065
    @wayneedmondson1065 2 года назад +1

    Thanks Paul! Very helpful in understanding more about Class Modules. Thanks for sharing. Thumbs up!!

  • @BenLinfordUK
    @BenLinfordUK 11 месяцев назад

    Revisiting your class videos and I have to say, that the syntax and inner workings is finally starting to click.
    My praise and thanks as always Paul. 👏

  • @peterlilley6819
    @peterlilley6819 2 года назад +1

    Thanks for the amazing videos Paul. I really like this approach, for those interested, I like to see the Items tree nested under the CollectionExt Class instance in the Expression window e.g. a tree node that breaks open showing Item 1, Item 2, etc. To achieve this I changed the following statement at the top of the CollectionExt class from "Private m_coll as New Collection" to "Public Items as New Collection". This is particularly helpful when assigning objects to properties!

  • @maciejolejnik6706
    @maciejolejnik6706 2 года назад +1

    Clearly explained. Thanks!

  • @joaocustodio2094
    @joaocustodio2094 2 года назад

    Paul, just great. Thanks very much.

  • @edgarsantarosa9847
    @edgarsantarosa9847 2 года назад +2

    Class modules made my scraping through football websites easier, because I get the full data and then I print it out with the changes I wanted and even faster because I use array to range print. Thanks Paul for the hint in your videos, Merry Xmas to you all!

  • @elsheep6951
    @elsheep6951 2 года назад

    Thanks Paul, really useful and practical.

  • @kissisvarmas
    @kissisvarmas 2 года назад

    Thank you Paul, Very informative

  • @smiqpk
    @smiqpk 2 года назад +2

    As usual you have explained it quite brilliantly, however, could you please mention a few examples of its real world implementation or use cases

  • @jimfitch
    @jimfitch 2 года назад

    Thank you, Paul. Great tutorial! I’m still building comfort in writing Class Modules. Your tutorial have inspired confidence.

  • @exceltech1076
    @exceltech1076 2 года назад

    Fantastic idea. Great Job. Keep it up.

  • @xf9639
    @xf9639 2 года назад

    Thanks Paul

  • @prakashsrinivasan7840
    @prakashsrinivasan7840 2 года назад

    Your videos r excellent lam learning a lot from u in VBA Thank u.

  • @MrMallesh1
    @MrMallesh1 2 года назад

    very less video on vba class module ! yours are best Video ! , thanks for making it !

  • @7Denial7
    @7Denial7 2 года назад +1

    Thank you Paul for your excellent lessons! Thanks to you I came to finally understand how classes work. And I created a fully animated dynamic Form for my co workers to generate different documents out of source data base. I used classes to add and remove dynamically different animation properties to objects on the Form!!! And it looks so great that I Cant believe I made it! By the way, consider bringing up Raise Events and event creation in your future lessons please.

  • @tomharrington1453
    @tomharrington1453 2 года назад

    Love the video. I like the additional collection features. I might add a function to return the position of an item in the collection. Forgive me for quibbling-- I know its OK to bend the rules sometimes-- but doesn't the "exit for" command inside your loop violate the "always exit a loop, procedure, function at the bottom" rule?

  • @societyuser6659
    @societyuser6659 2 года назад

    Great work, very good explanation. It helped me lot in implementation of small projects. Thanks🙏🙏❤❤

  • @aNDy-qh1em
    @aNDy-qh1em 2 года назад +3

    Paul, may I just also propose an alternative to iteration in 'Contains' method that would search item by key. Perhaps this should be faster for big collections. In this case missing item key will return an error to be handled to return 'false' and 'true' if found.

    • @monochromaticspider
      @monochromaticspider 2 года назад

      This is generally how you want to work with collections in VBA, but it does come with the requirement that items are added to the collection with a key and that no two identical items can be added. This is mostly reasonable, as iterating collections is slow, but there could be times when you want to just pile things into your collection.

  • @sinus2220
    @sinus2220 2 года назад +3

    Very cool clip!
    Two questions:
    1) Isn't boolean false by default? Why do you use Contains = False?
    2) This one is more complex:
    Let's say you need to create an array of ranges (each range is single row / multiple columns).
    I've used two methods:
    for i = 1 to 5
    Set MyArr(i) = Range("A" & i & ":C" & i)
    Set MyArr(i) = Columns("A:C").Rows(i)
    Next i
    Now if I want to print just the first and third cell from any given row, *only* the array defined using first method allows me to print them in this manner (e.g. 4th row): debug.print MyArr(4)(1), MyArr(4)(3).
    Array created using the second method returns entire 4th row (3 columns) when I print MyArr(4)(1). Why is this so?

    • @Excelmacromastery
      @Excelmacromastery  2 года назад +6

      It is the default but I state it explicitly so that it is clear what Contains is. It is not obvious to everyone that the default is False especially if you use different languages.

  • @schymi841
    @schymi841 2 года назад

    Pure gold

  • @aNDy-qh1em
    @aNDy-qh1em 2 года назад

    Thank you, Paul! What is a nice extention for collection! Just, as far as 'Contains' method is concerned - your exampke works fine with string, in case the type of collection items is not string but other type like instance of class X. In such case we might need to pass the name of ID field of class X to the compare method.

    • @Excelmacromastery
      @Excelmacromastery  2 года назад +1

      Contains is only for strings. Further modification is needed for other types.

  • @balakumar.n4891
    @balakumar.n4891 2 года назад

    Thanks!Paul

  • @Fab2Mc
    @Fab2Mc 2 года назад

    Super 👍, 🎉, good morning 🌄

  • @bayurukmanajati1224
    @bayurukmanajati1224 2 года назад

    Oh, I see now. I remember have a class that gone wrong at some line in its private procedure.
    The error is 'subscript out of range'. But instead teleporting me to the error line, the 'Debug' buttons just teleporting me to the initial caller (set myClass = newClass). I confused and decided to add many breakpoints inside the class to kno where the error happens 😥.

  • @Tesla72PL
    @Tesla72PL Год назад

    How we can use collectionExt class when we want for example show the item by the number of key like: debug.print coll(1) because now it not work.

  • @henrikijonkoping4694
    @henrikijonkoping4694 2 года назад

    Can anybody help me? What use as an accountant do I have of working with modules?

  • @fnscooter
    @fnscooter 2 года назад

    Would vbBinaryCompare allow you to store integers or doubles, or is further modification required to make collectionExt work for things other than strings?

  • @davidunger7425
    @davidunger7425 2 года назад +1

    Thanks Paul for another excellent video, very clear explanations. Just for my own clarification I'd like to raise a minor point. In the class function "Contains", the first line sets Contains = FALSE. Isn't this line redundant, as Contains is automatically FALSE on entry, and only gets changed to True if a match is found? Just asking, thank you.

    • @Excelmacromastery
      @Excelmacromastery  2 года назад +1

      Yes. A function/variable will be automatically set to False on creation. However, it is better to show it explicitly in your code as it avoids confusion.

    • @davidunger7425
      @davidunger7425 2 года назад

      @@Excelmacromastery Thanks for the explanation Paul.

  • @khalidalisawi8037
    @khalidalisawi8037 2 года назад

    thanks for your video, I learned a lot. my excel doesn't accept the collectionext I tried a lot. my excel version is 2010

  • @afiqyahya3398
    @afiqyahya3398 2 года назад +1

    The more i learn python oop, the more it makes sense to me for class module in vba. Strange, aint it.?

  • @noviceprogrammer2011
    @noviceprogrammer2011 2 года назад

    Something looks strange in your code but somehow it works!
    These two lines are in the class collectionExt:
    Public Function Clone() As collectionExt
    Dim newColl As New collectionExt
    How come it's not recursive?

  • @marceljunioregondi5594
    @marceljunioregondi5594 2 года назад

    Didn't go far with the tutorial. I am a novice in VBA. What is you suggestion on where to start from please.

  • @maximusfootball6266
    @maximusfootball6266 2 года назад

    You rush too much next time take your time