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 ChoudharyRank #145 · 15.8K reputationPosted Feb 22, 2010, 1:31 AM EST
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.
j l68 reputationPosted Feb 22, 2010, 2:05 AM EST
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 ChoudharyRank #145 · 15.8K reputationPosted Feb 22, 2010, 3:07 AM EST
Can you please post your complete code here i don't how you accessing the file??
And also what error you are getting???
j l68 reputationPosted Feb 22, 2010, 3:22 AM EST
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 ChoudharyRank #145 · 15.8K reputationPosted Feb 24, 2010, 9:15 PM EST
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.
Amit ChoudharyRank #145 · 15.8K reputationPosted Feb 24, 2010, 9:36 PM EST
try this: