Finding the volume of a tank in VBA given the radius, height, and depth

Viewed 2227

I am having trouble finding the volume of a tank for an Excel VBA course; I am to make a VBA function, which I input the radius, height, and depth of the tank, and receive the volume. Here is a picture for a better understanding:

The tank function instructions

This is the code I currently have, which the course tells me is incorrect:

    Option Explicit

Function tank(R As Double, d As Double, H As Double) As Double

Dim pi As Double

pi = Application.WorksheetFunction.pi()

If d <= R Then

tank = pi * d ^ 2 / 3 * (3 * R - d)

ElseIf R < d And d <= H - R Then

tank = 2 / 3 * pi * R ^ 3 + pi * R ^ 2 * (d - R)

ElseIf H - R < d And d <= H Then

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

End If

End Function

Could anyone help me with this problem? (I am using a macro provided by my course instructor, which does not tell me what is wrong, but if it is wrong or correct.)

1 Answers

As answered by dwirony in the comments, my assignment called for tank(R,H,d) and my function was tank(R,d,H) which changed the outcome. Thank you for everyone who helped, the function now works. Here is the working code:

Option Explicit

Function tank(R As Double, H As Double, d As Double) As Double

Dim pi As Double

pi = Application.WorksheetFunction.pi()

If d <= R Then

tank = pi * d ^ 2 / 3 * (3 * R - d)

ElseIf R < d And d <= H - R Then

tank = 2 / 3 * pi * R ^ 3 + pi * R ^ 2 * (d - R)

ElseIf H - R < d And d <= H Then

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

End If

End Function
Related