我在这里要做的是看看术语价值等于什么并从那里开始。 如果它是性病,那么我只需要将日期添加到发票日期; 如果它的BONM(下个月开始)我需要它在发票日期添加天数,然后作为答案参考下个月的开头。 如果它的EOM(月底)我需要它在发票日期添加天数,然后引用当月的结束。我附上了一张我正在收到的错误图片。尝试以不同的方式调暗它,但它仍然无法正常工作。
Function OldMaturity(term As Range, invoicedate As Range, days As Range) As Date
Dim term As String
Dim invoicedate As Date
Dim days As Long
Dim val1 As Long
Dim val2 As Long
term = Termtype.Value
invoicedate = invoicedate.Value
days = days.Value
val1 = val1.Value
val2 = val2.Value
If term = "STD" Then
OldMaturity = invoicedate + days
Exit Function
End If
If term = "BONM" Then
val1 = invoicedate + days
val2 = DateAdd("m", 1, val1)
OldMaturity = DateSerial(Year(val2), Month(val2), 1)
Exit Function
End If
If term = "EOM" Then
val1 = invoicedate + days
OldMaturity = DateSerial(Year(val1), Month(val1) + 1, 0)
Exit Function
End If
End If
End Function
答案 0 :(得分:3)
试试这个:
Public Function OldMaturity(term As Range, invoicedate As Range, days As Range) As Date
Dim d As Date
Select Case term.Value
Case "STD":
OldMaturity = DateAdd("y", days.Value, invoicedate.Value)
Case "BONM":
d = DateAdd("y", days.Value, invoicedate.Value)
OldMaturity = DateAdd("m", 1, DateSerial(Year(d), Month(d), 1))
Case "EOM":
d = DateAdd("y", days.Value, invoicedate.Value)
OldMaturity = DateAdd("y", -1, DateAdd("m", 1, DateSerial(Year(d), Month(d), 1)))
Case Else:
'do nothing
End Select
End Function
Sub Test()
'invoicedate: 15/03/2017
'days: 10
Debug.Print "STD: " & OldMaturity(Range("A1"), Range("B1"), Range("c1"))
Debug.Print "BONM: " & OldMaturity(Range("A2"), Range("B2"), Range("c2"))
Debug.Print "EOM: " & OldMaturity(Range("A3"), Range("B3"), Range("c3"))
End Sub
'Output:
'STD: 25/03/2017
'BONM: 01/04/2017
'EOM: 31/03/2017
答案 1 :(得分:1)
根据@Vincent G提到的内容,您无需重新定义函数中的参数。此外,您最后还有一个end if
声明。
编辑:发现了一个错误。如果term =" STD"然后OldMaturity返回类型不是Date。
Function OldMaturity(term As Range, invoicedate As Range, days As Range) As Date
'variables
Dim val1 As Long
Dim val2 As Long
term = Termtype.Value
invoicedate = invoicedate.Value
days = days.Value
val1 = val1.Value
val2 = val2.Value
If term = "STD" Then
OldMaturity = invoicedate + days
Exit Function
End If
If term = "BONM" Then
val1 = invoicedate + days
val2 = DateAdd("m", 1, val1)
OldMaturity = DateSerial(Year(val2), Month(val2), 1)
Exit Function
End If
If term = "EOM" Then
val1 = invoicedate + days
OldMaturity = DateSerial(Year(val1), Month(val1) + 1, 0)
Exit Function
End If
End Function