Paweł Szuszkiewicz
Paweł Szuszkiewicz

Reputation: 21

VBA Radius of a circle

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

Hey all so im trying to do a simple VBA programme to calculate radius of circle. Where just a box appears . you enter the radius and answer is given back in a msgbox. when

Upvotes: 0

Views: 2567

Answers (2)

Pinky
Pinky

Reputation: 1

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

Upvotes: -1

Ricardo Diaz
Ricardo Diaz

Reputation: 5696

It’s best practice to add

Option Explicit

at the beginning of each module. This will ensure you’re declaring all variables and prevent most of typos.

After that, correct this line:

MsgBox asnwer

You have a typo. Should be

MsgBox answer

Also, you don’t need to declare this variables as parameters.

Sub area(p As Single, radius As Integer)

Replace that line for:

Sub area()

Also check some other adjustments I made.

Final code:

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

Upvotes: 3

Related Questions