Hi Guys
I'm just starting out with VBA so any help would be most appreciated.
I wanted I'd try to write a custom function which returns the percentage complete for a project based on today's date and the duration of the total project.
The last line of the code is what is causing the #value! error when I divide the Progress and Duration variables.
As a test, I've tried adding them together which gives me the correct answer.
So I know I must have done something wrong with either the variable types or dividing dates?
Public Function PercentComplete(StartDate As Date, EndDate As Date) As Double
'Define variables
Dim Duration As Date
Dim Progress As Date
Dim TodaysDate As Date
'Set TodaysDate variable to current date
'Now() is a VBA function which returns current system date
TodaysDate = Now()
'Work out number of dayes between start date and end date
Duration = Application.WorksheetFunction.Days(EndDate, StartDate)
'Work out number of dayes between todays date and end date
Progress = Application.WorksheetFunction.Days(TodaysDate, StartDate)
'trying to divide progress by duration to give a percentage complete
PercentComplete = Progress / Duration
End Function
I'm just starting out with VBA so any help would be most appreciated.
I wanted I'd try to write a custom function which returns the percentage complete for a project based on today's date and the duration of the total project.
The last line of the code is what is causing the #value! error when I divide the Progress and Duration variables.
As a test, I've tried adding them together which gives me the correct answer.
So I know I must have done something wrong with either the variable types or dividing dates?
Public Function PercentComplete(StartDate As Date, EndDate As Date) As Double
'Define variables
Dim Duration As Date
Dim Progress As Date
Dim TodaysDate As Date
'Set TodaysDate variable to current date
'Now() is a VBA function which returns current system date
TodaysDate = Now()
'Work out number of dayes between start date and end date
Duration = Application.WorksheetFunction.Days(EndDate, StartDate)
'Work out number of dayes between todays date and end date
Progress = Application.WorksheetFunction.Days(TodaysDate, StartDate)
'trying to divide progress by duration to give a percentage complete
PercentComplete = Progress / Duration
End Function