I want to change the color of a cell in Excel bases upon the text in the cell being the value "TRUE"
I tried this statement, but I get an error...can someone help me?
I am using Microsoft.Office.Interop.Excel
if (excelApp.Range[rowIndex, colIndex].Value = "TRUE")
{
excelApp.Range[rowIndex, colIndex].Interior.ColorIndex = 4;
}
else
{
excelApp.Range[rowIndex, colIndex].Interior.ColorIndex = 3;
}
thanks
J
Loading
Amit ChoudharyPosted Feb 25, 2010, 2:36 AM
try this:
Amit ChoudharyPosted Feb 25, 2010, 2:15 AM
you have to change the indexing scheme as follows:
excelApp.Workbooks.Open(myPath);
int rowIndex = 1;
char colIndex = 'A';
excelApp.Cells[colIndex+rowIndex,colIndex+rowIndex] = "True";
if (excelApp.Range[colIndex + rowIndex, colIndex + rowIndex].Value = "True")
excelApp.Range[colIndex + rowIndex, colIndex + rowIndex].Interior.ColorIndex = 4;
else
excelApp.Range[colIndex + rowIndex, colIndex + rowIndex].Interior.ColorIndex = 3;
now you can have increment in the colindex in a special manner. Increment colIndex value with 1 so that it goes like A,B,C... when it comes to last Z then you have to start like AA,AB,AC,AD,...... etc.
Hope it helps you.
j lPosted Feb 22, 2010, 8:22 AM
I get an exception error at the "IF" statement line.....if (excelApp.Range[rowIndex, colIndex].Value = "TRUE")
What I want to do:
If the cell value of A1 is the text "TRUE", then I want to change the cell background of A2 to color GREEN
thanks
J
Amit ChoudharyPosted Feb 22, 2010, 8:07 AM
Can you please post your complete code here i don't how you accessing the file??
And also what error you are getting???
j lPosted Feb 22, 2010, 7:05 AM
If the cell value of A1 is the text "TRUE", then I want to change the cell background of A2 to color GREEN
How do I do this?
J
Amit ChoudharyPosted Feb 22, 2010, 6:31 AM
below code will solve your problem... i have tested it.
oSheet.get_Range("A1", "AV1").Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Green);
Don't forget to mark as answer if it helps you.