Article ID: 198112 - Last Review: March 21, 2005 - Revision: 3.2

How To Convert Indices (row,col) into Excel-Style A1:C1 Strings

This article was previously published under Q198112
Expand all | Collapse all

SUMMARY

Sometimes it is necessary to convert numerical indices (row,column) into Excel-style "A1:C1" string notation. This can be confusing because after "Z," Excel starts using "BA," "BB," etc. This article contains a function you can use in your code to do this conversion for you.

MORE INFORMATION

C++ code:

   // Converts (row,col) indices to an Excel-style A1:C1 string
   char *IndexToString(int row, int col, char *strResult)
   {
      if(col > 26)
         sprintf(strResult, "%c%c%d",
            'A'+(col-1)/26-1, 'A'+(col-1)%26, row);
      else
         sprintf(strResult, "%c%d", 'A' + (col-1)%26, row);

      return strResult;
   }

				
Some examples of the conversion:
Calling IndexToString() with row=1 and col=26 yields "Z1"
Calling IndexToString() with row=1 and col=27 yields "AA1"
Calling IndexToString() with row=2 and col=52 yields "AZ2"
Calling IndexToString() with row=2 and col=53 yields "BA2"
Calling IndexToString() with row=10 and col=10 yields "J10"

APPLIES TO
  • Microsoft Visual C++ 4.0 Standard Edition
  • Microsoft Visual C++ 5.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 5.0 Professional Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
Keywords: 
kbhowto KB198112
 

Article Translations