查找日期是在未来还是在过去

时间:2014-10-23 21:26:11

标签: vba date

'checks to see if date is within x days of the inputted date
Function isFutureDate(x) As Boolean   
    Dim daysFuture As Integer
    Dim futureDate As Date
    daysFuture = Sheet1.Range("e1").Value - 1
    'we have to add one to not count today
    futureDate = WorksheetFunction.WorkDay(Date, daysFuture) 
    If x >= Date And x < futureDate Then
        isFutureDate = True
    Else
        isFutureDate = False
    End If
End Function

'checks to see if date is in the past x days
Function isPastDate(x) As Boolean   
    Dim BDate As Date
    Dim y As Date
    Dim daysPast As Integer
    'subtract one to not count today
    daysPast = Sheet1.Range("E1").Value - 1 'subtract one to not count today

    BDate = WorksheetFunction.WorkDay(Date, -1 * daysPast)
    If x < Date And x > BDate Then
        isPastDate = True
    Else
        isPastDate = False
    End If
End Function

这是我目前的两个功能。 x作为日期传递。当我单步执行该程序时,我注意到BDate中的isPastDate function错误。在我的文件中,我有一个单元格,用户可以在其中输入将来要查看条目的天数。我认为这是我的主要问题所在。当我检查daysFuture或daysPast的值时,我得到0,而用户输入的值显然是7。

1 个答案:

答案 0 :(得分:0)

 'checks to see if date is within x days of the inputted date
    Function isFutureDate(x As Date) As Boolean
        Dim xDays As Integer
        Dim xFuture As Integer
        xDays = Sheet1.Range("E1").Value
        xFuture = DateDiff("d", Date, x)
        If xFuture > xDays Then
            isFutureDate = True
        Else
            isFutureDate = False
        End If
    End Function
    'checks to see if date is in the past x days
    Function isPastDate(x As Date) As Boolean
        Dim xDays As Integer
        Dim xPast As Integer
        xDays = Sheet1.Range("E1").Value
        xPast = DateDiff("d", Date, x)
        If xPast < xDays Then
            isPastDate = True
        Else
            isPastDate = False
        End If
    End Function