将日期与列表进行比较 & Return 列表中的下一个日期

Compare Date to List & Return Next Date In List

我带着另一个我不太确定该怎么做的东西回来了。如前所述,我是初学者 VBA 程序员,所以很多事情对我来说并不明显。

无论如何我想做的是:

到目前为止我有以下代码:

伟大工作的一部分sheet 事件 selection 事件

ElseIf Not Intersect(Target, Range("LastDayWorkedRange")) Is Nothing Then
       Call Cutoff2015
End If

哪个调用这个子:

Sub Cutoff2015()

'If Paygroup has not been selected when entering termination date then error message and enable events
If Range("PayGroupRange") = "Please Select" Then
        MsgBox "Error: Please select Paygroup"
        Application.EnableEvents = True

    'If paygroup is selected then
    Else: Select Case Range("PayGroupRange")
        Case 118
        MsgBox "118"

        Case 113
        MsgBox "113"

    End Select

End If

End Sub

截止日期在一个名为 "Cuttoff Matrix" 的单独 sheet 上,并且在第一行中使用支付组编号进行格式化,从第二行开始是按时间顺序排列的日期。案例中的 MsgBox 被放入以确保其工作。到目前为止,它确实如此。

假设您有一个命名范围 TerminationDate 并且 sheet "Cuttoff Matrix" 中的日期按升序排列,以下函数将为您生成与下一个日期进行比较的 msgbox到输入 Range("TerminationDate").

的日期
Private Function Next_Date()
Dim rng As Range
Dim rngFoundPayGroup As Range
Dim cell As Range

'setting the range in Cutoff Matrix from A1 to the last not empty cell to right
Set rng = ThisWorkbook.Sheets("Cuttoff Matrix").Range("A1")
Set rng = Range(rng, rng.End(xlToRight))

'finding identified PayGroup in the previously set range
Set rngFoundPayGroup = rng.Find(Range("PayGroupRange"), , xlValues, xlWhole)
'Looking downwards for the date bigger than termination date
For Each cell In Range(rngFoundPayGroup, rngFoundPayGroup.End(xlDown))
    If cell.Value > Range("TerminationDate").Value Then
        MsgBox cell.Value
        Exit For
    End If
    'If no date which is bigger, this msgbox appears
    If cell.Address = rngFoundPayGroup.End(xlDown).Address Then MsgBox "Next date not found"
Next

Set rngFoundPayGroup = Nothing
Set rng = Nothing
End Function

将此函数插入​​同一个模块,并将之前的行 MsgBox "118"MsgBox "118" 更改为 Call Next_Date。或者更好的是,插入功能代码后,删除你的这一部分:

Select Case Range("PayGroupRange")
    Case 118
    MsgBox "118"
    Case 113
    MsgBox "113"
End Select

并改用它:Call Next_Date