Add Module in Excel VBA Library in Java

This article guides how to add module in Excel VBA library in Java. It has the details to set the development environment, a list of programming tasks, and a sample code that demonstrates the addition of a module in VBA code library in Java. You will also learn to set various module properties, such as the VBA code that exhibits the development of a macro for an Excel file.

Steps to Modify Excel VBA Code Library in Java

  1. Set the IDE to use Aspose.Cells for Java to work with VBA
  2. Instantiate a Workbook object, access a sheet, and add a module for the selected sheet
  3. Access the new module from the workbook and set its name
  4. Write and test a VBA code and use it as the codes property in the module
  5. Save the output Excel file as XLSM on the disk or stream if required

These steps summarize the process of making additions to the Excel VBA source code library in Java. The process is simple as it requires a worksheet from a workbook to add a new VbaModule and then set a few properties in it. Set the property Codes with multi-line VBA code to make macro operational.

Code to Add Code in Excel Macro Library in Java

This code snippet shows how to access VBA code library in Java and add a module with code in it. In this sample code, we have set the name and code that changes the color of cells according to the range of values defined in the VBA code. You can add a module using another overloaded method that takes the VbaModuleType enumerator value as an argument that includes CLASS, DESIGNER, DOCUMENT, and PROCEDURAL.

This article has taught us to create a new macro in an Excel file. To apply conditional formatting in Excel, refer to the article on Apply conditional formatting in Excel using Java.

 English