excel 使用VBA基于频率计算到期日

ibps3vxo  于 2023-05-30  发布在  其他
关注(0)|答案(2)|浏览(186)

所以,现在我有这个Excel表格,其中有一个 * 最后修订 * 日期。我将此列命名为“LastRevisionDate”。然后我有一个名为“RevisionFrequency”的列。“RevisionFrequency”包含一个下拉菜单(数据验证),由术语AnnuallySemi-AnnuallyQuarterly组成。然后我有一个列,它指出了“NextRevisionDate”。
所以我想写一些VBA代码,从 LastRevisionDateRevisionFrequency 计算 NextRevisionDate
举个例子。假设在列“A”中,我的RevisionFrequency为Semi-Annually,并且在列“B”中的最后修订日期为Mar-14,那么我希望列“C”中的 NextRevisionDateSeptember。这基本上是说,该项目得到修订,每年两次。
因此,我想创建一个宏,其中列“C”基于 RevisionFrequencyLastRevisionDate。我意识到我可以用公式来做到这一点,但我有新的项目不断添加,所以我不想不断复制公式到每个单元格。此外,对于一些项目,他们不需要修订,我也希望有一个空白单元格,如果没有 LastRevisionDate
到目前为止,我有这样的代码:

Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
 Set ws = Sheets(1)
'For this reference of the Column Named LastCalDate I am getting an error 
If Not Intersect(Target, ws.Range("LastCalDate").Value) Is Nothing Then

Dim Lastdate As Date
 Dim DueDate As Variant
 Dim Frequency As String
 Dim R As Variant
 Dim C As Variant
 Dim R1 As Variant
 Dim C1 As Variant
 Dim R2 As Variant
 Dim C2 As Variant



R = Range("LastCalDate").Row
 C = Range("LastCalDate").Column

R1 = Range("CalDueDate").Row
 C1 = Range("CalDueDate").Column

R2 = Range("CalFrequency").Row
 C2 = Range("CalFrequency").Column

Lastdate = Cells(R, C).Value 'Last Cal Date
 DueDate = Cells(R1, C1).Value 'Cal Due Date
 Frequency = Cells(R2, C2)

If Frequency = "Annually" Then

DueDate = DateAdd("mmm", 12, Lastdate)

End If

If Frequency = "Semi-Annually" Then
 DueDate = DateAdd("mmm", 6, Lastdate)
 End If

If Frequency = "Quarterly" Then
 DueDate = DateAdd("mmm", 3, Lastdate)
 End If


End Sub

这是我目前所知道的。我不确定我这样做是否正确。

igsr9ssn

igsr9ssn1#

使用Worksheet_Change方法是创建新单元格值而无需复制和粘贴公式的好方法。我还在代码中包含了检查,以确保如果未设置日期或频率,则清除该值。

Private Sub Worksheet_Change(ByVal Target As Range)

' declare and set worksheet
Dim ws As Worksheet
Set ws = Sheets(1)

' declare and set default date
Dim DefaultDueDate As Date

' declare needed variables
Dim StartDate As Date
Dim Frequency As String
Dim DueDate As Date

' make sure the change only occured on the "A" or "B" column
If Target.Column = 1 Or Target.Column = 2 Then

    StartDate = ws.Range("A" & Target.Row)
    Frequency = ws.Range("B" & Target.Row)

    ' if start date does not equal the default due date and the frequency is not blank, set due date variable
    If StartDate <> DefaultDueDate And Frequency <> "" Then

        ' add months to the provided start date
        If Frequency = "Annually" Then
            DueDate = DateAdd("m", 12, StartDate)
        ElseIf Frequency = "Semi-Annually" Then
            DueDate = DateAdd("m", 6, StartDate)
        ElseIf Frequency = "Quarterly" Then
            DueDate = DateAdd("m", 3, StartDate)
        End If

        ' Make sure frequency selection is correct and due date was set
        If DueDate <> DefaultDueDate Then
            ws.Range("C" & Target.Row) = DueDate
        End If

    Else

        ' clear Next Revision Date when Frequency or Start Date is blank
        ws.Range("C" & Target.Row) = ""

    End If

End If

End Sub
mnemlml8

mnemlml82#

更改代码如下,它正在工作。
专用子工作表_变更(ByVal目标范围)
'声明并设置工作表尺寸% ws为工作表集% ws = Sheets(2)
'声明并设置默认日期Dim DefaultDueDate As Date
' declare needed variables Dim StartDate As Date Dim PeriodType As String Dim DueDate As Date Dim PeriodValue As Long
'确保更改仅发生在“A”或“B”列如果目标.列= 6或目标.列= 7或目标.列= 8则

StartDate = ws.Range("F" & Target.Row)
PeriodType = ws.Range("G" & Target.Row)
PeriodValue = ws.Range("H" & Target.Row)

' if start date does not equal the default due date and the frequency is not blank, set due date variable
If StartDate <> DefaultDueDate And PeriodType <> "" Then

    ' add months to the provided start date
    If PeriodType = "Year" Then
        DueDate = DateAdd("YYYY", PeriodValue, StartDate)
    ElseIf PeriodType = "Quarter" Then
        DueDate = DateAdd("q", PeriodValue, StartDate)
    ElseIf PeriodType = "Month" Then
        DueDate = DateAdd("m", PeriodValue, StartDate)
    ElseIf PeriodType = "Week" Then
        DueDate = DateAdd("ww", PeriodValue, StartDate)
    ElseIf PeriodType = "Day" Then
        DueDate = DateAdd("d", PeriodValue, StartDate)
    End If

    ' Make sure frequency selection is correct and due date was set
    If DueDate <> DefaultDueDate Then
        ws.Range("K" & Target.Row) = DueDate
    End If

Else

    ' clear Next Revision Date when Frequency or Start Date is blank
    ws.Range("K" & Target.Row) = ""

End If

如果结束
结束子

相关问题