help! function not obtaining cell values

M

maweilian

Newbie question:

The simple VBA function below is not working. It is called by the following
in the spreadsheet:
=calcsoilpress(D9,D10,'Soil Data'!D7,'Soil Data'!E7,D17)

(note: "Soil Data" is another worksheet in the same xls file)

When I add a breakpoint at the first "If-Then" line and examine the value of
the variables, I discovered that some of the parameters had a value of zero.
But the value of the cells are not zero! What is happening here?

Thanks in advance for any help!

Will

See code below:


Public Function calcsoilpress(soiltop As Double, soilbottom As Double,
soilwtdensitydry As Double, soilwtdensitywet As Double, waterdepth As Double)

If waterdepth <= soiltop Then
calsoilpress = (soilbottom - soiltop) * soilwtdensitywet
End If

If waterdepth >= soilbot Then
calsoilpress = (soilbottom - soiltop) * soilwtdensitydry
End If

If waterdepth > soiltop And waterdepth < soilbot Then
calsoilpress = ((soilbottom - waterdepth) * soilwtdensitywet) +
((waterdepth - soiltop) * soilwtdensitydry)
End If

End Function
 
M

maweilian

Thanks so much. Problem solved! I feel pretty silly. Its amazing how your
mind can play tricks on you.
 
P

Patrick Molloy

placing
OPTION EXPLICIT
at the start of the module will help . under Tools/Options under the Editor
tab, make sure that 'Require Variable Declaration' is checked. This is great
for trapping those typos.
Also, before running code, use the Debug / Compile menu item to do a quick
check in case something obvious is broken
 
Top