使用VBA进行数据验证

时间:2013-04-19 05:47:44

标签: excel excel-vba validation vba

我正在为以下代码获取“应用程序定义或对象定义的对象错误”

10        Sheets("Main").Select
20        currYearDate = DateValue("April 1, " & Year(Now))
30        nextYearDate = DateValue("March 31, " & (Year(Now) + 1))
40        Range("H9").Activate
50        With ActiveCell.Validation
60            .Add Type:=xlValidateDate, AlertStyle:=2, Operator:=1, Formula1:=currYearDate, Formula2:=nextYearDate
70            .ErrorMessage = "You should enter date between " & Format(DateValue("April 1," & Year(Now)), "dd-mmm-yyyy") & " and " & Format(DateValue("March 31," & Year(Now) + 1), "dd-mmm-yyyy")
80            .ErrorTitle = "Warning Message"
90        End With

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

尝试以下代码:

 Sheets("Main").Select
    currYearDate = DateValue("April 1, " & Year(Now))
    nextYearDate = DateValue("March 31, " & (Year(Now) + 1))
    With Range("H9").Validation
        .Delete
        .Add Type:=xlValidateDate, AlertStyle:=2, Operator:=1, Formula1:=currYearDate, Formula2:=nextYearDate
        .ErrorMessage = "You should enter date between " & Format(DateValue("April 1," & Year(Now)), "dd-mmm-yyyy") & " and " & Format(DateValue("March 31," & Year(Now) + 1), "dd-mmm-yyyy")
        .ErrorTitle = "Warning Message"
    End With