L
lostintheloop
Hi --
I'm trying to write a simple C# program that iterates through an array
of strings and finds the unique cell in a spreadsheet in which each
string resides.
For example, if my array is {"A," "B", "C"}, I'm trying to find the
particular cell with the value A, then the cell with the value B, then
the cell with the value C. In each case, I want to write out the
cell's contents.
I've set up a simple foreach loop that calls Range.Find. The loop
processes the first element of the array correctly, but when it gets to
the second element, I get an error saying that my object reference
("state" in the code below) isn't set to an instance of an object.
As far as I can tell, there's some subtlety associated with Range.Find
that I'm overlooking that causes it to misbehave on the second
iteration.
If anyone has any ideas as to what I'm doing wrong, I'd greatly
appreciate hearing from you.
Thanks in advance for your help.
*************************************************************
foreach (string stateName in stateNames)
{
Excel.Range worksheetRange = (Excel.Range)ws.UsedRange;
Excel.Range state;
Excel.Range statefoundfirst = null;
state = worksheetRange.Find(stateName, Type.Missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
Type.Missing, Type.Missing);
while(state != null)
{
if (statefoundfirst == null)
statefoundfirst = state;
else if (state.get_Address(Type.Missing, Type.Missing,
Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing) ==
statefoundfirst.get_Address(Type.Missing, Type.Missing,
Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing))
break;
state = worksheetRange.FindNext(state);
}
Console.WriteLine("\t" + "/* " + state.Text + " */");
}
I'm trying to write a simple C# program that iterates through an array
of strings and finds the unique cell in a spreadsheet in which each
string resides.
For example, if my array is {"A," "B", "C"}, I'm trying to find the
particular cell with the value A, then the cell with the value B, then
the cell with the value C. In each case, I want to write out the
cell's contents.
I've set up a simple foreach loop that calls Range.Find. The loop
processes the first element of the array correctly, but when it gets to
the second element, I get an error saying that my object reference
("state" in the code below) isn't set to an instance of an object.
As far as I can tell, there's some subtlety associated with Range.Find
that I'm overlooking that causes it to misbehave on the second
iteration.
If anyone has any ideas as to what I'm doing wrong, I'd greatly
appreciate hearing from you.
Thanks in advance for your help.
*************************************************************
foreach (string stateName in stateNames)
{
Excel.Range worksheetRange = (Excel.Range)ws.UsedRange;
Excel.Range state;
Excel.Range statefoundfirst = null;
state = worksheetRange.Find(stateName, Type.Missing,
Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole,
Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false,
Type.Missing, Type.Missing);
while(state != null)
{
if (statefoundfirst == null)
statefoundfirst = state;
else if (state.get_Address(Type.Missing, Type.Missing,
Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing) ==
statefoundfirst.get_Address(Type.Missing, Type.Missing,
Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing))
break;
state = worksheetRange.FindNext(state);
}
Console.WriteLine("\t" + "/* " + state.Text + " */");
}