XLL+ Class Library

Find/Replace

Our next example of a macro function makes use of the CXllFinder class.

Note: the full code for this example can be found in the project FindReplace in the Samples sub-directory.

Create the function

Create a new function FindReplace() as shown below.

Amend the code

Amend the generated code as shown below, and build the project.

//{{XLP_SRC(FindReplace)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(FindReplace, "A", "FindReplace", "", "User Defin"
    "ed", "Demonstrate find/replace", "", "", 2)

extern "C" __declspec( dllexport )
BOOL FindReplace()
{
//}}XLP_SRC

    const char* apszPairs[] = {
        "Fred", "Freda",
        "George", "Georgina",
        0
    };

    // Iterate through each find/replace task
    for (int i = 0; apszPairs[i]; i += 2) {
        // Create a finder
        CXllFinder finder(apszPairs[i], FALSE, TRUE);

        // Alternatively - create a finder for a specific sheet
        //CXllFinder finder(apszPairs[i], FALSE, TRUE, "FindReplace.xls", "Sheet2");

        // Look for each match
        CXlRef xlrFind;
        while (finder.FindNext(xlrFind)) {
            // Inspect the current value
            CXlOper xloValue;
            finder.GetCurrentValue(xloValue);
            TRACE("%s\n", (LPCSTR)xloValue.ToString());
            // Change the value in the matched cell
            finder.SetCurrentValue(apszPairs[i + 1]);
        }
    }

    return 0;
}

Run the macro

Build the project, and start Excel.

Type the values "George" and "Fred" into several cells.

Using the Alt-F8 key (or with one of the other methods described in the last topic), run the FindReplace macro. Note that each of the Georges and Freds turn to their feminine equivalents.

Next: CXlMacros class >>