下面是代碼,儘管如何清除我的錯誤?從字符串「MM/yyyy」到「Date」類型的轉換無效
Public Function AddNextMonthDue(ByRef OldNMD As String, ByRef MonthsToAdd As String) As String
Dim dOldNMD As Date
Dim dNewNMD As Date
Dim sOldNMD As String
If Len(OldNMD) = 4 Then ' MMDD format
sOldNMD = "20" & Right(OldNMD, 2) & Left(OldNMD, 2) & "01"
Else
sOldNMD = OldNMD ' YYYYMMDD format
End If
dOldNMD = CDate(Mid(sOldNMD, 5, 2) & "/1/" & Left(sOldNMD, 4))
dNewNMD = DateAdd(Microsoft.VisualBasic.DateInterval.Month, CShort(MonthsToAdd),dOldNMD)
AddNextMonthDue = Format(CDate(dNewNMD), "MMyy")
End Function
你的錯誤是什麼? – Jay
爲什麼你將MonthsToAdd作爲字符串傳遞?如果你需要一個Short,那麼傳入Short。 –