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_XLLFN3(MyAddress, MyAddress_4, MyAddress_12, "RR", "UU", L"MyAddress", 0, L"Range", 0, L"14", 0, L"Returns the address of the input range", 0, L"Input range\0", 0, 0, L"{MyAddress,,,Returns the address of the input ra" L"nge,14,1,128,U,{{0,{Range,Reference,0,,Input range,,,,}}},{},3,,0,0}", 1) CXlOper* MyAddress_Impl(CXlOper&, const CXlOper*); extern "C" __declspec(dllexport) LPXLOPER12 MyAddress_12(LPXLOPER12 Range) { XLL_FIX_STATE; CXlOper xloResult, Range__port(Range); try { CXlStructuredExceptionHandler _seh_; xloResult.HandleResult(MyAddress_Impl(xloResult, &Range__port)); } catch(const CXlRuntimeException& ex) { CXllApp::Instance()->DisplayException(xloResult, ex); } return xloResult.Ret12(); } extern "C" __declspec(dllexport) LPXLOPER4 MyAddress_4(LPXLOPER4 Range) { XLL_FIX_STATE; CXlOper xloResult, Range__port(Range); try { CXlStructuredExceptionHandler _seh_; xloResult.HandleResult(MyAddress_Impl(xloResult, &Range__port)); } catch(const CXlRuntimeException& ex) { CXllApp::Instance()->DisplayException(xloResult, ex); } return xloResult.Ret4(); } CXlOper* MyAddress_Impl(CXlOper& xloResult, const CXlOper* Range) { // End of generated code //}}XLP_SRC // Is the argument a reference ? if ( Range->IsRef() ) { // Build up the full address of each range // eg "[Book1.xls]Sheet1!A1:B1" CString sAddress, sSheetName; // Start with the sheet name sSheetName = Range->GetSheetName() + "!"; // Count the number of ranges WORD cRefs = Range->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 + Range->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(); }
CXlOper::IsRef | CXlOper::GetSheetName | CXlOper::GetRefCount | CXlOper::GetRefItem | CXlRef::ToString