# How do I get the Index of a known cell?

Hi

I need to get the index of a cell so that I can fill a range with a sequential series of values.

If the cell is “G5”, how do I get the Index of that cell?

– the cell definitely has an index because it has a value already - I need to replace the cell value.

You can direclty use the cell name to access it.

For example,

excel.Worksheets.Cells[“G5”].PutValue(“Hello World”);

I know that, but I need the cell index.

What I want to do is add an incremental value (1 to 52) starting from cell G5.
So here is the series…

G5 - 1
G6 - 2
G7 - 3
etc.

I thought the easiest way of doing this is a loop using the cell’s index.
Is there an easier way?

Now you can try the following approach:

int startIndex = -1;
for(int i = 0; i < cells.Count; i ++)
{
Cell cell = cells ;
if(cell.Name = “G5”)
{
startIndex = i;
break;
}
}

if(startIndex != -1)
{
for(int i = startIndex; i < startIndex + 52; i ++)
{
Cell cell = cells ;
//do something here

}
}

In future verion, I can add a new GetIndex method, then you can simplify your code to:

Cell cell = cells[“G5”];
int startIndex = cells.GetIndex(cell);
for(int i = startIndex; i < startIndex + 52; i ++)
{
Cell cell = cells ;
//do something here

}

Thanks for that, although I figured out an alternative method.

A GetIndex method would be very useful in future! 