How to create validation from name range on another worksheet in excel using C#?

人走茶凉 提交于 2020-01-14 03:49:07

问题


I have create name range on sheet "A" so I need to use this range as validation ComboBox on sheet B. I want to know how can I setting validation as range using C#?


回答1:


The sheets won't matter because you'll just reference the named range of the list value range. Here you go below (assumes Interop) - listValidatingRange is where you have your values that need to be displayed in the dropdown - add that as a named range. cellThatNeedsValidating is the cell that you want the drop-down to appear in - add that as a named range. Then, on cellThatNeedsValidating, add the validation to be that of "=ListValidatingRange".

private void SetValidation()
{

    Microsoft.Office.Tools.Excel.NamedRange listValidatingRange =
        this.Controls.AddNamedRange(this.Range[""C1:C13"", missing],
        "ListValidatingRange");

    Microsoft.Office.Tools.Excel.NamedRange cellThatNeedsValidating =
        this.Controls.AddNamedRange(this.Range[""A1"", missing],
        "cellThatNeedsValidating");

    cellThatNeedsValidating.Validation.Add(
        Excel.XlDVType.xlValidateList ,
        Excel.XlDVAlertStyle.xlValidAlertStop,
        missing, "=ListValidatingRange", missing);
}


来源:https://stackoverflow.com/questions/2414591/how-to-create-validation-from-name-range-on-another-worksheet-in-excel-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!