You need to delete the cell validator before adding another one. Otherwise you will see the validation Exception is thrown 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 no cell validator exists (ie first time) then Deleting doesn't cause a problem, its safe to leave in.
Edit/Solution:
The problem in the code was the variable arr
contained two items 1 & 100. I'm guessing the XLFormatConditionOperator argument xlBetween
in Validation.Add
's parameter mislead us. To make it work for a argument XLDVType of xlValidateList
the Formula1 argument needs to contain all the 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);
}
}