我无法找到Excel VBA课程的水箱容积;我要做一个VBA功能,我输入了水箱的半径,高度和深度,并接收体积。这是一张更好理解的图片:
这是我当前拥有的代码,课程告诉我这是不正确的:
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
有人可以帮助我解决这个问题吗? (我使用的是我的课程讲师提供的宏,该宏不会告诉我什么地方有问题,但是会告诉我错误还是正确的。)
答案 0 :(得分:5)
正如dwirony在评论中回答的那样,我的任务要求使用tank(R,H,d),而我的职能是tank(R,d,H),它改变了结果。感谢所有提供帮助的人,该功能现在可以使用了。这是工作代码:
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