excel VBA圆的半径

nr9pn0ug  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(120)
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。当

643ylb08

643ylb081#

最好的做法是

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
unhi4e5o

unhi4e5o2#

Sub test3()
Dim radius as double
Dim area as double
Radius = 4
Area = 3.14*radius^2
 
'Here the ^symbol is used to raise radius to the power 2

Debug.print " value of radius is: "& radius
Debug.print " value of radius is: "& area
End sub

相关问题