kingofaces
Board Regular
- Joined
- Aug 23, 2010
- Messages
- 68
I'm working on writing a function for growing degree days (sine wave if anyone is familiar) and I have just one little snag that's been bothering me. The sheet is set up with two columns, minimum and max temperatures and the formula SinehalfDD is used for each row. The problem I have is that when the min and max temps are equal, the cell returns #VALUE! for that row, otherwise everything else is working fine. My VBA knowledge is just pieced together from scratch, so I'm guessing this has something to do with how I set my variables up? This example from the start of the whole thing shows the problem I'm running into.
Code:
Function SinhalfDD(Min, Max, Base, Ceiling) As Double
Dim Taverage As Double
Dim W As Double
Dim PTheta1 As Double
Dim PTheta2 As Double
Taverage = (Max + Min) / 2
W = (Max - Min) / 2
PTheta1 = (Base - Taverage) / W
PTheta2 = (Ceiling - Taverage) / W
....
....
End Function
I've narrowed it down to this where say I input 30 for both the min and max values. I could create another variable from something simple like = Max + Min and it would return 60 just fine. However, once I ask for the value of Taverage to be returned, that's when I get the VALUE error. Obviously (30+30)/2 should be 30. This is only for identical values though. If I had 60 for a max and 30 for a min all of the variables listed above would have regular numbers and the rest of the formula would work just fine. I have also tried just setting up an If statement to set SinehalfDD = 0 outright if the max and min are equal, but that still returns the error, so I figure it has to be something outside the formula I've made and rather an issue with the variables themselves. Any pointers are greatly appreciated as this is the only special case left I need to work out before I can use it.
Windows XP/ Excel 2003
Code:
Function SinhalfDD(Min, Max, Base, Ceiling) As Double
Dim Taverage As Double
Dim W As Double
Dim PTheta1 As Double
Dim PTheta2 As Double
Taverage = (Max + Min) / 2
W = (Max - Min) / 2
PTheta1 = (Base - Taverage) / W
PTheta2 = (Ceiling - Taverage) / W
....
....
End Function
I've narrowed it down to this where say I input 30 for both the min and max values. I could create another variable from something simple like = Max + Min and it would return 60 just fine. However, once I ask for the value of Taverage to be returned, that's when I get the VALUE error. Obviously (30+30)/2 should be 30. This is only for identical values though. If I had 60 for a max and 30 for a min all of the variables listed above would have regular numbers and the rest of the formula would work just fine. I have also tried just setting up an If statement to set SinehalfDD = 0 outright if the max and min are equal, but that still returns the error, so I figure it has to be something outside the formula I've made and rather an issue with the variables themselves. Any pointers are greatly appreciated as this is the only special case left I need to work out before I can use it.
Windows XP/ Excel 2003
Last edited: