I write a code with C# and Microsoft.Office.Interop.Excel
. In generated a drop-down list where one can chose between "okay", "delete", "else". Now I want that the cell becomes red if "delete" is chosen. I tried to do this with Excel.FormatCondition
:
Excel.FormatCondition condition = mysheet.get_Range("J2:J10",
Type.Missing).FormatConditions.Add(Type:Excel.XlFormatConditionType.xlTextString,
Operator: Excel.XlFormatConditionOperator.xlEqual, Formula1: "=\"delete\"");
condition.Interior.ColorIndex = 3;
With this code I get an error:
System.Runtime.InteropServices.COMException: 'Exception from HRESULT: 0x800A03EC'
Old question, but hopefully this might help someone else. When you set the type to Type:Excel.XlFormatConditionType.xlTextString
, you need to set the String:
and TextOperator:
parameters and not Operator:
and Formula1:
as shown in the question.
Fixing the code:
FormatConditions.Add(Type:Excel.XlFormatConditionType.xlTextString,
TextOperator: Excel.XlContainsOperator.xlContains, String: "delete");
I have found the solution. It is straight forward. I had to use XlFormatConditionType.xlCellValue
instead of TextString:
//Drop-down List
var list = new System.Collections.Generic.List<string>();
list.Add("okay");
list.Add("delete");
list.Add("else (Comment)");
var flatList = string.Join(",", list.ToArray());
var celldd = (Excel.Range)mysheet.Cells[row_counter, 10];
celldd.Validation.Delete();
try
{
int type = celldd.Validation.Type;
Console.WriteLine("The validation have already been added");
}
catch (System.Runtime.InteropServices.COMException)
{
celldd.Validation.Add(
Excel.XlDVType.xlValidateList,
Excel.XlDVAlertStyle.xlValidAlertInformation,
Excel.XlFormatConditionOperator.xlBetween,
flatList, misValue);
}
celldd.Validation.InCellDropdown = true;
celldd.Validation.IgnoreBlank = true;
celldd.Locked = false;
celldd.Value2 = "Choose";
celldd.Interior.Color = ColorTranslator.ToOle(Color.Plum);
//Conditional Formatting
Excel.FormatCondition condition1 = mysheet.get_Range("J2:J10",
Type.Missing).FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "=\"delete\"", misValue, misValue, misValue, misValue, misValue);
condition1.Interior.ColorIndex = 3; //red
Excel.FormatCondition condition2 = mysheet.get_Range("J2:J10",
Type.Missing).FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "=\"okay\"", misValue, misValue, misValue, misValue, misValue);
condition2.Interior.ColorIndex = 43; //green
Excel.FormatCondition condition3 = mysheet.get_Range("J2:J10",
Type.Missing).FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "=\"else (Comment)\"", misValue, misValue, misValue, misValue, misValue);
condition3.Interior.ColorIndex = 44; //yellow
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.