XLL+ Class Library (6.3)

Setting cell values

Let us create a simple macro function that puts the current time in cell A1 of the current worksheet.

Create the function

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

Add the code

Amend the generated code as shown below.

CopyC++
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.

Running the macro

We can invoke the macro in a number of ways. Let us examine a few of them.

  1. Directly

    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.

  2. With a menu or toolbar

    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.

  3. Attached to a button in a workbook

    Using the Forms toolbar, create a new button.

    In the Assign Macro dialog, type in "SETDATE", and click OK.

  4. With code

    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:

    CopyVBA
    Call Application.Run("SETDATE")
    Call Application.Run("AnotherMacroFunction", 12.6, "cheese")

Next: Find/Replace >>