Let us create a simple macro function that puts the current time in cell A1 of the current worksheet.
Create a macro function in one step using the New Macro command, which is located on the toolbar of the XLL Add-ins window.
Name: SETDATE
Amend the generated code as shown below.
CXlOper* SETDATE_Impl(CXlOper& xloResult) { // End of generated code //}}XLP_SRC // Make a reference to cell A1 CXlOper xloRef; xloRef.MakeRef("A1"); // Set the value of cell A1 xloRef.SetValue(CXlDate::Now()); return xloResult.Ret(); }
Build the project and open the add-in in Excel.
We can invoke the macro in a number of ways. Let us examine a few of them.
Invoke the Run Macro dialog box, either by clicking the Tools - Macro - Macros... menu option or with Alt+F8.
Type in the macro name "SETDATE", as shown below, and click Run.
You can create menus and toolbars that will appear when your add-in is opened and be destroyed when the add-in is closed. Each menu item and toolbar tool can be attached to a named macro function.
Menu and toolbars are discussed in detail in Menus and Toolbars.
Using the Forms toolbar, create a new button.
In the Assign Macro dialog, type in "SETDATE", and click OK.
You can also run a macro from VBA code, using the Application.Run() method. For example, the following lines invoke macros with and without arguments:
Call Application.Run("SETDATE") Call Application.Run("AnotherMacroFunction", 12.6, "cheese")