0

我有以下代码可以使用自定义文本进行 Excel 单元格验证。

var cellValues = activeSheet.Range[columnLetterValue + startingRow, Type.Missing];
cellValues.Validation.Add(XlDVType.xlValidateCustom, XlDVAlertStyle.xlValidAlertInformation, XlFormatConditionOperator.xlBetween, cellValues.Value2, Type.Missing);
cellValues.Validation.IgnoreBlank = true;
cellValues.Validation.ErrorTitle = "Custom error title";
cellValues.Validation.ErrorMessage = "Custom error message description";
cellValues.Validation.ShowError = true;
cellValues.Validation.ShowInput = false;

在调试设置 ErrorTitle 或 ErrorMessage 的行时,'System.Runtime.InteropServices.COMException'会抛出一个。

堆栈跟踪:

{System.Runtime.InteropServices.COMException (0x800A03EC):来自 HRESULT 的异常:
System.RuntimeType.ForwardCallToInvokeMember 的 0x800A03EC(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) 在
Microsoft.Office.Interop.Excel。 Validation.set_ErrorTitle(字符串)

我在网上查看并发现了几个例子表明这是可能的: 使用 C# 使用 Excel 进行范围验证

我什至在 Excel 中录制了一个宏,它与“VBA”等效代码一起使用:

    ActiveCell.FormulaR1C1 = "Custom"
    Range("A1").Select
    With Selection.Validation
        .Delete
        .Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertInformation, _
        Operator:=xlBetween, Formula1:="Custom"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = "UDF Title"
        .InputMessage = ""
        .ErrorMessage = "The message"
        .ShowInput = False
        .ShowError = True
    End With
End Sub 

I've also tried different combinations of XlDVType and XlFormatConditionOperator but that didn't seem to make any difference in setting the custom messagebox text.

Does anyone know how to set a Validation rule with custom ErrorTitle and ErrorMessage?

4

1 回答 1

1

The problem was due to setting a Validation.ErrorTitle to a range that didn't have a Validation rule.

于 2012-09-21T03:31:21.993 回答