VBA圆弧半径

时间:2019-03-03 14:37:33

标签: excel vba

Sub area(p As Single, radius As Integer)
Dim answer As Single



' area of circle is 3.14 x r(sq)


Const p = 3.14

answer = p * radius^

radius = InputBox("Enter the radius")




area = answer

MsgBox asnwer

End Sub

嘿,我一直试图做一个简单的VBA程序来计算圆半径。仅出现一个框的地方。您输入半径,然后在msgbox中给出答案。何时

1 个答案:

答案 0 :(得分:3)

最佳做法是添加

Option Explicit

在每个模块的开头。这样可以确保您声明所有变量并避免大多数错字。

之后,请更正以下行:

MsgBox asnwer

您有错字。应该是

MsgBox answer

此外,您无需将此变量声明为参数。

Sub area(p As Single, radius As Integer)

将该行替换为:

Sub area()

还请检查我进行的其他一些调整。

最终代码:

Option Explicit
Sub Area()

    Dim answer As Double
    Dim radius As Double

    ' area of circle is 3.14 x r(sq)

    ' Define PI constant
    Dim p As Double

    p = WorksheetFunction.Pi()

    ' Ask for the radius
    radius = InputBox("Enter the radius")

    ' Calculate area
    answer = p * radius ^ 2

    ' Show the user the answer
    MsgBox answer

End Sub