简体   繁体   中英

To many items for Excel drop down list when using validation.add from c#

In my c# project, which creates xlsx file, there is a rows in code, which deletes and then sets validation for a range of cells:

VedomostSheet.Range[VedomostSheet.Cells[VedomostStartRow, Convert.ToInt16(GetCellValue(rowDic, 1, DicSheet))], VedomostSheet.Cells[20000, Convert.ToInt16(GetCellValue(rowDic, 1, DicSheet))]].Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, values, Type.Missing);

For this row of code debugger throws an error:

System.Runtime.InteropServices.COMException HResult=0x800A03EC Message=Exception from HRESULT: 0x800A03EC Source= Cannot evaluate the exception source StackTrace: Cannot evaluate the exception stack trace

As I found out this is because there are too many values for a drop down list, which are stored in values variable of Validation.Add() function.

How can I overcome the limit on the number of values for a drop-down list when adding validation?

Here is the solution(thanks to @Rory):

Range myrange;//creating the range variable with all values for drop down list
myrange = DicSheet.Range[DicSheet.Cells[2, Convert.ToInt16(GetCellValue(rowDic, 2, DicSheet))], DicSheet.Cells[row-1, Convert.ToInt16(GetCellValue(rowDic, 2, DicSheet))]];
//using this variable with the name of a excel sheet:
VedomostSheet.Range[VedomostSheet.Cells[VedomostStartRow, Convert.ToInt16(GetCellValue(rowDic, 1, DicSheet))], VedomostSheet.Cells[20000, Convert.ToInt16(GetCellValue(rowDic, 1, DicSheet))]].Validation.Add(XlDVType.xlValidateList, XlDVAlertStyle.xlValidAlertStop, XlFormatConditionOperator.xlBetween, "='" + DicSheet.Name + "'!" + myrange.Address, Type.Missing);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM