MACRO LIBRARY

Lesson 1.8 homework solution

Free public access. No account or subscription required.

Files

Lesson 1.8 homework solution code

Code

Modules: HW1_81

'Homework 1.8
'Turn Homework 1.7b into a user form, with radio buttons for the calculation types, a textbox _
    to enter the radius, and a command button to perform the calculation.  Instead of _
    retyping the function modules from HW1.7b, export them as .bas files and then import them _
    into this new macro project.  Also, simplify the original by removing unnecessary variables _
    and error handling.
    
Sub main()
    UserForm1.Show
End Sub

Modules: modSurfaceArea

Function SurfaceArea(radius As Double) As Double
    SurfaceArea = 4 * 4 * Atn(1) * radius ^ 2
End Function

Modules: modVolume

Function Volume(radius As Double) As Double
    Volume = (4 / 3) * 4 * Atn(1) * radius ^ 3
End Function

Forms: UserForm1

Private Sub cmdCalculate_Click()
    Dim dblRadius As Double
    
    If IsNumeric(txtRadius.Text) = False Then
        MsgBox "Radius must be a number."
        Exit Sub
    End If
    dblRadius = CDbl(txtRadius.Text)
    
    If optSurfaceArea.Value = True Then
        MsgBox "The surface area is: " & Round(SurfaceArea(dblRadius), 3)
    Else
        MsgBox "The volume is: " & Round(Volume(dblRadius), 3)
    End If
End Sub