HRESULT exception: 0x800A03EC when adding data validation

int row_index = 2;
foreach (DataRow row in dataTableSourceSystem.Rows)
{
    xlWorkSheet.get_Range("AM" + row_index.ToString(), System.Reflection.Missing.Value).Value2 = row["LookupShortName"].ToString();
    row_index++;
}
Range range = xlWorkSheet.get_Range("G2,G50");

range.Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, "=$AM2:$AM50", System.Reflection.Missing.Value);

The last line throws an exception 0x800A03EC

Am2up Am50does matter.

Can anybody help?

+3
source share
2 answers

It should be

Range range = xlWorkSheet.get_Range("G2:G50");

You have G2,G50where the comma appears to be the "concatenated" operator. Instead, you need a range operator (see here ) according to an AM2: AM50 range of the same size.

0
source

I also had this exception, and I understood it because of Worksheet.Selection.
I have not figured out how this relates to Data Validation, but I will look at it and let you know.

To solve your problem, you should do something like this:

 xlWorkSheet.Range["$A$1"].Select();
 Range range = xlWorkSheet.get_Range("G2,G50");
 Validation v = range.Validation;

 // you might want to delete validation first
 // v.Delete();

 v.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, "=$AM2:$AM50", System.Reflection.Missing.Value);

 // some other useful stuff for you
 // v.ErrorMessage = "You need to fill in a value from the validation list.";
 // v.InCellDropdown = true;
0

All Articles