Solved

convert integers columns & rows into CString Excel cell format.

Posted on 2000-05-12
3
644 Views
Last Modified: 2009-07-29
Hi all,
I am searching for an algorithm which converts integers Columns and Rows, in an Excel cell.

E.g. if I have nCol=0, nRow=0 => algo returns CString "A1"
if I have nCol=28, nRow=1  => algo must return "AB2" (2 chars wide for Col description)

Thanks a lot !

Regards,
David.
0
Comment
Question by:tradinfo
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
marcjb earned 50 total points
ID: 2804399
I think that nCol = 28, nRow = 1 should be "AC2".  If that is the case, the following is a C program that has a function to do what you want.

You would have to change the character array stuff to a CString, but the algorithm is the same.  If the column is greater than 25 (since we are using 0 as the base), then there are two letters to describe the column.


#include <stdio.h>

void toExcel(int i, int j, char *s)
{
if ( i > 25 )
    {
    sprintf(s, "%c%c%1i", 'A' + (i/26) - 1, 'A' + (i%26), j + 1);
    }
else
    sprintf(s, "%c%1i", 'A' + i, j + 1 );
}

int main(void)
{
int x, y;
char a[5];

x = 0;
y = 0;
toExcel(x, y, (char*)a);
printf("x: %i  y: %i  a: %s\n", x, y, a);

x = 28;
y = 1;
toExcel(x, y, (char*)a);
printf("x: %i  y: %i  a: %s\n", x, y, a);

return 0;
}

0
 

Author Comment

by:tradinfo
ID: 2804602
Cool code.
Here it is translated in MsVC++ :

void C_Results::ConvCell (long nCol, long nRow, CString& strCell)
{
      CString strCol = "";
      if (nCol>25)
      {      strCol = ((char)('A' + (nCol/26) - 1));
            strCol += ((char)('A' + (nCol%26)) );
      }
      else
            strCol = (char)('A' + nCol);
            
      strCell.Format("%s%d", strCol, nRow + 1 );
}

Thanks.
0
 
LVL 3

Expert Comment

by:marcjb
ID: 2804689
Glad to help :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now