XLL+ Class Library

MyAddress Example

This example demonstrates how to examine and manipulate Excel cell addresses.

// Function:    MyAddress
// Returns:     LPXLOPER
// Description: Returns the address of the input range

//{{XLP_SRC(MyAddress)
    // NOTE - the FunctionWizard will add and remove mapping code here.
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN2(MyAddress, "RR", "MyAddress", "Range", 
    "User Defined", "Returns the address of the input range", 
    "Input range\000", "\0", 1)

extern "C" __declspec( dllexport )
LPXLOPER MyAddress(const CXlOper* lpxlopRange)
{
    CXlOper xloResult;
//}}XLP_SRC

    // Is the argument a reference ?
    if ( lpxlopRange->IsRef() )
    {
        // Build up the full address of each range
        // eg "[Book1.xls]Sheet1!A1:B1"
        CString sAddress, sSheetName;
        // Start with the sheet name
        sSheetName = lpxlopRange->GetSheetName() + "!";
        // Count the number of ranges
        WORD cRefs = lpxlopRange->GetRefCount(), nRef;
        // Add each of the ranges
        for ( nRef = 0; nRef < cRefs; nRef++ )
        {
            // Put a comma between each range
            if ( nRef > 0 )
                sAddress += ",";
            // Append the address of each reference as a string;
            // the TRUE argument to ToString indicates that A1 
            // style references should be used, rather than R1C1.
            sAddress += sSheetName 
                     + lpxlopRange->GetRefItem(nRef).ToString(TRUE);
        }
        // Copy the address to the result CXlOper
        xloResult = sAddress;
    }
    // If the argument is not a reference, return #N/A
    else
        xloResult = xlerrNA;

    return xloResult.Ret();
}

Uses

CXlOper::IsRef | CXlOper::GetSheetName | CXlOper::GetRefCount | CXlOper::GetRefItem | CXlRef::ToString