打印截止日期到期日,并给出范围

时间:2016-12-24 12:41:02

标签: vba

我希望代码在给定范围时打印截止日期。

例如,如果截止日期是每个月的第一天,而我给出的范围是5/1/12和5/23/16,那么我想要的答案应该是6/1 / 12,7 / 1 / 12,8 / 1 / 12,9 / 1/12至2016年5月1日,格式应为 mmddyyyy

1 个答案:

答案 0 :(得分:0)

您没有指定要在哪个程序中使用它,或者您想如何激活它,所以我只想假设Microsoft Excel或Microsoft Access,因为您需要在VBA中使用它。我将此编写为可以在Excel公式或Access查询或未绑定文本框中使用的函数。我也确信一个更优雅的解决方案是可能的,但是从我已经完成的测试中,这是有效的。

Here is an example of use in Excel

以下是该功能的代码清单:

Option Explicit


Public Function GetNextDueDate(DateRangeBegin As Date, DateRangeEnd As Date, DayDue As Byte) As Variant
    ' •Purpose•
    '     Given a date range, and a day of the month as a due date, return the first due date that is
    '     after the beginning of the date range and today, and on or before the end of the date range.
    '
    ' •Comments•
    '     There is a fair amount of data validation necessary to ensure the return of the proper due date.
    '     The function currently returns a text string representing an error when there is a problem with
    '     finding a valid due date. You may wish to change this to use the Err.Raise method if you want
    '     the code engine to actually generate an error.

    Dim TempDueDate As Date

    On Error GoTo HandleErr_GetNextDueDate

    'Check that date range is valid (i.e. begin date is not after end date).
    If DateRangeBegin > DateRangeEnd Then
        'Return error text.
        GetNextDueDate = "#ERROR_INVALID_DATE_RANGE"
    Else
        'Determine today's relation to the given date range.
        Select Case Date
            Case Is <= DateRangeBegin
                'Date range begins today or is in the future. Verify that DayDue falls after DateRangeBegin and on or before DateRangeEnd.
                TempDueDate = DateSerial(Year(DateRangeBegin), Month(DateRangeBegin), DayDue)
                If TempDueDate > DateRangeBegin Then
                    If TempDueDate <= DateRangeEnd Then
                        'Day due falls within the specified range.
                        GetNextDueDate = TempDueDate
                    Else
                        'There is no valid due date within the given date range.
                        GetNextDueDate = "#ERROR_NO_VALID_DUE_DATE"
                    End If
                Else
                    'Day due for the same month as DateRangeBegin falls before DateRangeBegin. Check for the following month.
                    TempDueDate = DateSerial(Year(DateRangeBegin), Month(DateRangeBegin) + 1, DayDue)
                    If TempDueDate <= DateRangeEnd Then
                        'Day due falls within the specified range .
                        GetNextDueDate = TempDueDate
                    Else
                        'There is no valid due date within the given date range.
                        GetNextDueDate = "#ERROR_NO_VALID_DUE_DATE"
                    End If
                End If
            Case Else
                'Date range has already begun, or is in the past. Check if today's date is on or after DateRangeEnd.
                If Date >= DateRangeEnd Then
                    'See if the last due date before DateRangeEnd is valid.
                    TempDueDate = DateSerial(Year(DateRangeEnd), Month(DateRangeEnd), DayDue)
                    If TempDueDate <= DateRangeEnd Then
                        'Temp date is valid for end of date range. Check if valid for beginning.
                        If TempDueDate > DateRangeBegin Then
                            'Temp date is last valid due date.
                            GetNextDueDate = TempDueDate
                        Else
                            'There is no valid due date within the given date range.
                            GetNextDueDate = "#ERROR_NO_VALID_DUE_DATE"
                        End If
                    Else
                        'DayDue for the same month as DateRangeEnd is after DateRangeEnd. Check for the previous month.
                        TempDueDate = DateSerial(Year(DateRangeEnd), Month(DateRangeEnd) - 1, DayDue)
                        'Verify that temp date is after DateRangeBegin.
                        If TempDueDate > DateRangeBegin Then
                            'Temp date is last valid due date.
                            GetNextDueDate = TempDueDate
                        Else
                            'There is no valid due date within the given date range.
                            GetNextDueDate = "#ERROR_NO_VALID_DUE_DATE"
                        End If
                    End If
                Else
                    'Today's date falls within the specified date range. Attempt to get valid due date after today.
                    If DayDue > Day(Date) Then
                        TempDueDate = DateSerial(Year(Date), Month(Date), DayDue)
                        'Check that TempDueDate is on or before DateRangeEnd.
                        If TempDueDate <= DateRangeEnd Then
                            GetNextDueDate = TempDueDate
                        Else
                            'DayDue for the current month is past DateRangeEnd. Determine if DayDue prior to DateRangeEnd is valid.
                            TempDueDate = DateSerial(Year(Date), Month(DateRangeEnd) - 1, DayDue)
                            If TempDueDate > DateRangeBegin Then
                                GetNextDueDate = TempDueDate
                            Else
                                'There is no valid due date within the given date range.
                                GetNextDueDate = "#ERROR_NO_VALID_DUE_DATE"
                            End If
                        End If
                    Else
                        'Today's date is on or past the DayDue for this month. Try for next month.
                        TempDueDate = DateSerial(Year(Date), Month(Date) + 1, DayDue)
                        'Check that TempDueDate is on or before DateRangeEnd.
                        If TempDueDate <= DateRangeEnd Then
                            GetNextDueDate = TempDueDate
                        Else
                            'DayDue for next month is past DateRangeEnd. Verify that due date for this month is after DateRangeBegin.
                            TempDueDate = DateSerial(Year(Date), Month(Date), DayDue)
                            If TempDueDate > DateRangeBegin Then
                                GetNextDueDate = TempDueDate
                            Else
                                'There is no valid due date within the given date range.
                                GetNextDueDate = "#ERROR_NO_VALID_DUE_DATE"
                            End If
                        End If
                    End If
                End If
        End Select
    End If

ExitGetNextDueDate:
    Exit Function
HandleErr_GetNextDueDate:
    MsgBox "Error: " & Err.Number & vbCrLf & vbCrLf & Err.Description,     vbCritical, "An Error Occured During GetNextDueDate Function"
    Resume ExitGetNextDueDate

End Function

我希望这对你有所帮助。