2015-11-13 49 views
0

我想验证在单元格中输入的日期,但我遇到了问题。该场景是我有一个命名的单元格(列标题)范围,其日期视图上保存日期。然后用户输入开始和结束日期,我想验证: 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 

我错过了什么,或者我想做的事情只是不会工作?

+0

你能提供的开始日期和结束日期范围样品 – newjenn

+0

例如,假设日历开始日期为15年1月1日和日历结束日期是15年12月31日。我需要测试用户输入的日期对这两个。因此,如果用户将开始日期设置为1/1/15,则用户开始日期为1/1/14时不会产生任何问题,则会导致错误。同样,2/1/15的用户结束日期可以,但2/1/16是错误的。最后,如果用户输入的开始日期晚于用户输入的结束日期,那也会导致错误。 – Brian

回答

1

下面是修改做一个自定义的验证您的代码段。你正在做一个Excel内置的Date验证,如你注意到的,只能验证一个条件。

在Excel中使用验证多个条件你需要使用自定义的验证(xlValidateCustom而不是xlValidateDate)。当您使用此类型时,您需要创建一个公式并将其分配给“Formula1”属性。

正如你可以看到,使用多个条件,我只是用一个自定义的式与“=和(条件1,条件2 [,...])”功能。只有当所有条件返回true时,'=和()'函数才会返回true。

'=and()' function documentation

' Variables for the start and end dates of the calendar range 
Dim sd As Date 
Dim ed As Date 
' create string variables to store range addresses 
Dim sdRange as string 
Dim edRange as string 
Dim start_dtRange as string 
Dim end_dtRange as string 
' create string variable to store our custom formula 
Dim formula as string 

' 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)) 
' store the range addresses of named ranges 
sdRange = Range("weeks_rg")(1).Address 
edRange = Range("weeks_rg")(Range("weeks_rg").Columns.Count).Address 
start_dtRange = Range("start_dt").Address 
end_dtRange = Range("end_dt").Address 
' store our custom formula in a string 
formula = "=and(" & start_dtRange & ">" & sdRange & "," & start_dtRange & "<" & edRange & "," & start_dtRange & "<" & end_dtRange & ")" 

' Validate that ('start date' > sd and < ed and < 'end date') 
With Range("start_dt").Validation 
.Delete ' needed to delete any existing validation 
.Add Type:=xlValidateCustom, _ 
AlertStyle:=xlValidAlertStop, _ 
Formula1:=formula 
'Modify ErrorTitle and ErrorMessage to have appropriate content 
.ErrorTitle = "Start Date" 
.ErrorMessage = "You must enter a Start Date within the calendar date range of " & sd & " and " & ed 
End With 
+0

帕特里克,谢谢你!这让我更加接近,我想我可以让它工作。它仍然只是测试一个条件,这在公式中恰好有点复杂,但是有什么方法可以单独真正测试多个条件吗?理想情况下,我想要一种IF语句,以便我可以有多个测试/消息,具体取决于用户的开始日期是否在日期范围的开始之前;在用户结束日期之后;在日期范围结束之后,然后在用户的结束日期字段中进行相同类型的验证。 – Brian

+0

使用内置数据验证我认为您只能使用一个公式,并且每个单元格都有一个错误消息。您还有其他选择。我可以想到的选项是: (1)VBA工作表更改事件以测试每个条件(这可以确实减慢速度) (2)您可以添加自定义输入框/用户窗体并让用户输入值窗体并进行测试 –

+0

以下是指向文档的链接:[https://msdn.microsoft.com/EN-US/library/office/dn301144.aspx](https://msdn.microsoft.com/EN-美国/图书馆/办公室/ dn301144.aspx) –