Encapsulation

Class modules allow you to encapsulate code and data in such a way that it becomes very easy to use, very easy to share, and much easier to maintain.

You hide the code that does the work from the user, who only needs to know what sort of object the class module represents, and what properties and methods are associated with the object. This is particularly useful when it is necessary to make calls to the Windows API (application programming interface) to perform tasks that are not possible in normal VBA. This topic is presented in Chapter 27, where you can see examples that encapsulate very complex code and create very usable objects.

Class modules provide a mechanism for encapsulating code that you can use in other workbooks or share with other programmers to reduce development time. You can easily copy a class module to another workbook. In the Project Explorer window, it is as straightforward as dragging the class module between the projects.

You can also export the code in the class module to a file by right-clicking the module in the Project Explorer and choosing Export File to create a text file that can be copied to another PC. The file can then be imported into another workbook by right-clicking its project in the Project Explorer and choosing Import File.

So far, this chapter has examined class modules from a general programming perspective. You will now see how to use class modules to gain more control over Excel.

+1 -1

Responses

Post a comment