Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through an Excel range

Tags:

c#

excel

I am using Excel application SheetChange event to capture any change in my Excel app sheets. if a user modifies only 1 cell, then retrieving the cell coordinates can be done via:

void CellsChange(object Sh, Excel.Range Target)
{
 ....
 string changedCell = Target.get_Address(Missing.Value, Missing.Value, Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value);
 ....
}

In the above example a typical return value is "$C$11".

But if a user modifies a range of cells by highlighting more than one cell, and using shift-enter to fill the whole range with the same value, the returned string can be something like: "$C$11:$K$11" (indicating a 9 cell in-a-row change).

How can i iterate throw the range? getting each cell coordinate and value in a foreach or for loops. I tried the following...

for (int i = 0; i < Target.Count; i++)
{
   Excel.Range r = Target.Item[i];
   MessageBox.Show(Convert.ToString(r.Value2));
}

but this code is not giving me the original range cells new value. I also didn't follow the Target.Item logic - is it zero based array or one based array. On few tries it looked like the Item array is the whole sheet cell range formated as array (thus Item[0] can be used as well, which is one cell to the left of the highlighted range).

Does anyone have more experience using the Range object and/or the above event?

Thanks

like image 699
NirMH Avatar asked Jun 27 '11 19:06

NirMH


People also ask

How do you loop a range in Excel?

One way to loop through a range is to use the For... Next loop with the Cells property. Using the Cells property, you can substitute the loop counter (or other variables or expressions) for the cell index numbers. In the following example, the variable counter is substituted for the row index.

CAN YOU FOR loop in Excel?

The Microsoft Excel FOR... NEXT statement is used to create a FOR loop so that you can execute VBA code a fixed number of times. The FOR... NEXT statement is a built-in function in Excel that is categorized as a Logical Function.

Does Excel have a while loop?

The Excel Do While Loop function is used to loop through a set of defined instructions/code while a specific condition is true.


2 Answers

To iterate the Range, it's 1-based, that is:

for (int i = 1; i <= Target.Count; i++)
{
  Excel.Range r = (Excel.Range)Target.Item[i];
  MessageBox.Show(Convert.ToString(r.Value2));
}
like image 177
Ky Wu Avatar answered Sep 21 '22 17:09

Ky Wu


Since you are already getting the Range object in your event handler, you don't want to re-query the worksheet for your range--you won't get the new values.

Instead, try looping through the Range.Cells property, like this:

foreach (Range c in Target.Cells)
{
   string changedCell = c.get_Address(Type.Missing, Type.Missing, XlReferenceStyle.xlA1, Type.Missing, Type.Missing);  
   MessageBox.Show("Address:" + changedCell + " Value: " + c.Value2);
}
like image 36
Slider345 Avatar answered Sep 21 '22 17:09

Slider345