gpt4 book ai didi

excel - 给定半径、高度和深度,在 VBA 中查找水箱的体积

转载 作者:行者123 更新时间:2023-12-01 23:12:08 25 4
gpt4 key购买 nike

我无法找到 Excel VBA 类(class)的水箱体积;我要制作一个 VBA 函数,我输入水箱的半径、高度和深度,并接收体积。为了更好地理解,这是一张图片:

The tank function instructions

这是我目前拥有的代码,类(class)告诉我它是不正确的:

    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

谁能帮我解决这个问题? (我正在使用我的类(class)讲师提供的宏,它不会告诉我哪里出了问题,但会告诉我它是错的还是正确的。)

最佳答案

正如 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

关于excel - 给定半径、高度和深度,在 VBA 中查找水箱的体积,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56706232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com