需要根据开始日期但以可变增量计算到期日。
我在这个网站上找到了下面的代码,并添加了一些额外的标准。工作得很好,但它是固定的。如何使“数字”可变并从单元格中获取值?VBA纽比.
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)
ElseIf Frequency = "Month" Then
DueDate = DateAdd("m", 1, StartDate)
ElseIf Frequency = "Week" Then
DueDate = DateAdd("ww", 1, StartDate)
ElseIf Frequency = "Day" Then
DueDate = DateAdd("d", 1, 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
1条答案
按热度按时间km0tfn4u1#
1.添加一个名为Number的新变量来保存单元格中的值:
1.从特定单元格中检索“Number”的值。例如,如果包含“Number”值的单元格位于C列中,则可以用途:
1.更新执行频率计算的代码块,以使用Number变量而不是固定值:
确保调整ws.Range(“C”& Target.Row)中的列引用,以匹配存储“Number”值的实际单元格。