IsDate函数在数据验证vba中

时间:2015-08-03 04:11:57

标签: vba validation

我想对特定列进行数据验证,

我可以限制用户输入数字和文字 这样做的:

Sub Main()

    Dim rng As Range
    Set rng = Sheet1.Cells(1, 1)

    Select Case rng
        Case "Text"
            valFormulaText = "=istext(" & Range(rng.Offset(1, 0), rng.Offset(65535, 0)).Address(False, False) & ")"
            Range(rng.Offset(1, 0), rng.Offset(65535, 0)).Validation.Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=valFormulaText
        Case "Numeric"
            valFormulaNumeric = "=isnumber(" & Range(rng.Offset(1, 0), rng.Offset(65535, 0)).Address(False, False) & ")"
            Range(rng.Offset(1, 0), rng.Offset(65535, 0)).Validation.Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=valFormulaNumeric
        Case "Date"
            varFormulaDate = "=isdate(" & Range(rng.Offset(1, 0), rng.Offset(65535, 0)).Address(False, False) & ")"
            Range(rng.Offset(1, 0), rng.Offset(65535, 0)).Validation.Add Type:=xlValidateCustom, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=varFormulaDate
    End Select

End Sub

虽然运行时错误1004'

的日期案例有错误

2 个答案:

答案 0 :(得分:1)

Excel中有函数isnumber()和istext()。但isdate()不是。您应该在使用之前添加isdate函数。

答案 1 :(得分:0)

varFormulaDate = "=isdate" & ...

缺少“(isdate之后