VBA Function (Something wrong with my equation?)

BalbasNiBarabas

New Member
Joined
Jun 1, 2020
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Hello guys!

Would just like some verification on why am I getting the wrong value/answer to this.

Here's the problem:

If d <= R then:
tank = pi * d ^ 2 / 3 * (3 * R - d)

but if d<=h - r then:
tank = 2 / 3 * pi * R ^ 3 + pi * R ^ 2 * (d - H)

Else:
tank = 4 / 3 * pi * R ^ 3 + pi * R ^ 2 * (H - 2 * R) - pi * (H - R) ^ 2 / 3 * (3 * R - H + d)

Below is my vba code:

VBA Code:
Function tank(R As Double, H As Double, d As Double) As Double
Dim pi As Double
pi = WorksheetFunction.pi()
If d <= R Then
    tank = pi * d ^ 2 / 3 * (3 * R - d)
ElseIf d <= H - R Then
    tank = 2 / 3 * pi * R ^ 3 + pi * R ^ 2 * (d - H)
Else
    tank = 4 / 3 * pi * R ^ 3 + pi * R ^ 2 * (H - 2 * R) - pi * (H - R) ^ 2 / 3 * (3 * R - H + d)
End If
End Function

Let me know your thoughts.

Thank you!
 

Attachments

  • VBA code.png
    VBA code.png
    23.8 KB · Views: 9

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Sorry if I am completely wrong, because I am not very keen on calculations.
Did you look at the hierarchy of operations to separate them correctly with parentheses?
 
Upvote 0
Based on your posted graphic, your 2nd eqn

VBA Code:
tank = 2 / 3 * pi * R ^ 3 + pi * R ^ 2 * (d - H)

is using H where it should be using R.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top