我想验证在单元格中输入的日期,但我遇到了问题。该场景是我有一个命名的单元格(列标题)范围,其日期视图上保存日期。然后用户输入开始和结束日期,我想验证: 1)开始日期在命名范围内的日期范围内 2)开始日期在结束日期之前。VBA验证 - 一个单元格上的多个日期验证
如果日期是无效的,用户应提交的错误信息,应该有纠正问题。我已经能够让每个验证自行工作,但不能一起工作。
这里是我的作品来验证命名范围内的日期:
' Variables for the start and end dates of the calendar range
Dim sd As Date
Dim ed As Date
' Set the dates to the start and end of the calendar range
sd = DateValue(Range("weeks_rg")(1))
ed = DateValue(Range("weeks_rg")(Range("weeks_rg").Columns.Count))
' Validate that the start date is within the calendar range
With Range("start_dt").Validation
.Delete
.Add Type:=xlValidateDate, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlBetween, Formula1:=sd, Formula2:=ed
.ErrorTitle = "Start Date"
.ErrorMessage = "You must enter a Start Date within the calendar date range of " & sd & " and " & ed
End With
这很好地独立工作,因为做这个代码验证的开始日期是早于结束日期:
With Range("start_dt").Validation
.Delete
.Add Type:=xlValidateDate, _
AlertStyle:=xlValidAlertStop, _
Operator:=xlLessEqual, Formula1:=Range("end_dt")
.ErrorTitle = "Start Date"
.ErrorMessage = "You must enter a Start Date that is before the End Date"
End With
不过,我似乎无法让他们一起工作。我曾尝试将它们放入单独的子集中并按顺序调用它们,但这不起作用;只有第二次验证发生,第一次验证似乎被忽略。我也尝试将它们组合成一个With语句,但也忽略了第一个验证。
我敢肯定这是一件很小,因为我很新的VBA ...并只能远远得到这是由于在本网站上有很大的帮助:)什么我可以做,使这项工作任何想法?
======================
新增11/16: 我通过对功能有很多的在线帮助看了,但找不到很多似乎做我正在尝试做的事情。我想使用类似下面的代码,但它不起作用,我不知道为什么。即使调试语句显示逻辑正在工作,验证也不会触发。
的整体过程调用验证和主要程序,如果用户更改他们想查看的开始或结束日期:
Private Sub Worksheet_Change(ByVal Target As Range)
' This section calls the process utilization procedure when the user moves
' out of the start date or end date fields
If Target.Address = Range("start_dt").Address Then
Debug.Print "start changed"
Range("start_dt").Validation.Delete
Process_Utilization
Verify_start_date
End If
If Target.Address = Range("end_dt").Address Then
Debug.Print "end changed"
Range("end_dt").Validation.Delete
Process_Utilization
Verify_end_date
End If
End Sub
然后两个日期的程序看起来像这样:
Private Sub verify_end_date()
' Variables for the start and end dates of the calendar range
Dim sd As Date
Dim ed As Date
' Variables for the user-entered start and end dates
Dim ued As Date
Dim usd As Date
' create string variable to store our custom formula
Dim title As String
Dim msg As String
Debug.Print "start"
' Set the dates to the start and end of the calendar range
sd = DateValue(Range("weeks_rg")(1))
ed = DateValue(Range("weeks_rg")(Range("weeks_rg").Columns.Count))
' Set the user-entered dates
usd = DateValue(Range("start_dt"))
ued = DateValue(Range("end_dt"))
' Check if the user-entered end date is within the calendar range
If Not (sd <= ued And ued <= ed) Then
Debug.Print "ued out of range"
title = "End Date"
msg = "You must enter a Start Date within the calendar date range of " & sd & " and " & ed
With Range("end_dt").Validation
.Delete ' needed to delete any existing validation
.Add Type:=xlBetween, _
AlertStyle:=xlValidAlertStop, _
Formula1:=sd, Formula2:=ed
.ErrorTitle = title
.ErrorMessage = msg
End With
ElseIf ued < usd Then ' Check if the user end date is prior to the user start date
Debug.Print "end before start"
title = "End Date"
msg = "The End Date must be later than the Start Date"
With Range("end_dt").Validation
.Delete ' needed to delete any existing validation
.Add Type:=xlgretaerequal, _
AlertStyle:=xlValidAlertStop, _
Formula1:=usd
.ErrorTitle = title
.ErrorMessage = msg
End With
End If
End Sub
我错过了什么,或者我想做的事情只是不会工作?
你能提供的开始日期和结束日期范围样品 – newjenn
例如,假设日历开始日期为15年1月1日和日历结束日期是15年12月31日。我需要测试用户输入的日期对这两个。因此,如果用户将开始日期设置为1/1/15,则用户开始日期为1/1/14时不会产生任何问题,则会导致错误。同样,2/1/15的用户结束日期可以,但2/1/16是错误的。最后,如果用户输入的开始日期晚于用户输入的结束日期,那也会导致错误。 – Brian