简体   繁体   English

查找日期是将来的还是过去的

[英]Finding if a date is in the Future or in the Past

'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

These are the two functions I have currently. 这是我目前拥有的两个功能。 x is passed as a Date . x作为Date传递。 When I step through the program, I notice an error with BDate in the isPastDate function . 当我逐步执行程序时,我在isPastDate function注意到BDate错误。 In my file, I have a cell where the user enters how many days in the future they would like to see entries for. 在我的文件中,我有一个单元格,用户在该单元格中输入他们希望查看其条目的未来几天。 I think this is where my main problem is. 我认为这是我的主要问题所在。 When I check the value of daysFuture or daysPast I get 0 while the user entered value is clearly 7. 当我检查daysFuture或daysPast的值时,当用户输入的值明显为7时,我得到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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM