help! function not obtaining cell values

  • Thread starter Thread starter maweilian
  • Start date Start date
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
 
Thanks so much. Problem solved! I feel pretty silly. Its amazing how your
mind can play tricks on you.
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top