You need to remove the cell check item before adding another. Otherwise, you will see the check Exception selected as Exception from HRESULT: 0x800A03EC
ExcelApp.get_Range("A1").Cells.Validation.Delete(); ExcelApp.get_Range("A1").Cells.Validation.Add(Microsoft.Office.Interop.Excel.XlDVType.xlValidateList, Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertInformation, Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween, delimitedString1To100, Type.Missing);
If the cell validator does not exist (i.e. the first time), then deleting does not cause a problem; it is safe to leave it.
Change / Decision:
The problem in the code was an arr variable containing two elements 1 and 100. I assume that the argument XLFormatConditionOperator xlBetween in Validation.Add is misleading to us. To make it work for the XLDVType xlValidateList argument, the Formula1 argument must contain all valid values ββ1,2,3 ... 100:
var val = new Random(); var delimitedString1To100 = string.Join(",", (int[])Enumerable.Range(1, 100).ToArray()); for (int i = 1; i < 11; i++) { using (var rnCells = xlApp.Range["A" + i.ToString()].WithComCleanup()) { rnCells.Resource.Value2 = val.Next(100); rnCells.Resource.Cells.Validation.Delete(); rnCells.Resource.Cells.Validation.Add( Microsoft.Office.Interop.Excel.XlDVType.xlValidateList, Microsoft.Office.Interop.Excel.XlDVAlertStyle.xlValidAlertInformation, Microsoft.Office.Interop.Excel.XlFormatConditionOperator.xlBetween, delimitedString1To100, Type.Missing); } }
Jeremy thompson
source share