I am learning vba and now I am trying to start using classes because I see that otherwise my programs are too hard to work with and I always writing the similar pieces of code over and over.
Can you recommend any book that covers user-defined objects in vba well? Because everything that I found just gives a short introduction on this subject.
While I was searching for such a book, I found a lot of advanced books on vb.net. As I understood this language is more “powerful” (can be used for wider variety of tasks).
When you want to learn about VBA, look for references on VB6. You can probably find lots of references and books for cheap. I consider myself very good at VBA, and this is how I learned a lot. Even the newest version of VBA (VBA7) is esentially a subset of VB6. Most of what you can learn about VB6 is completely applicable to VBA, particularly in regards to classes and UDTs (which are kind of like
structsin the .NET world).From my own experience I would say that user-defined types (declared using the
Typekeyword) are rarely useful except for private use within in a single module for the purpose of native function declarations which require a pointer to a certain structure. Their fundamental drawback is that they cannot be cast to and fromVariant, which is a killer for advanced VBA. Once you get used to it it’s not too hard to quickly build a class (a class module in com VB lingo) with the same fields.Post script: This will not help you when learning about classes or user defined types, so I add this merely as a post script: You can also find lots of good information by searching for VBScript. Most VBScript can be pasted directly in to a VBA project and it will run, with small exceptions like not having access to the
Wscriptobject. Many of the documentation examples such as for MSXML (for interacting with both XML files and the internet) show VBScript, and all of this code can be readily used in VBA.