Wednesday, September 30, 2015

Right triangle and Circle calculator

This macro calculates "Area of a circle" and "Diameter" if you put a value in "Radius" box and choose "circle" in the combo-box. If you choose "Right triangle" in the combo-box and put a value of Base and Height, this calculates the Area and the length of the third-leg.



Interface is like this. The code is below.
------------------------
Private Sub UserForm_Initialize()

TextBox1.Value = 0
TextBox2.Value = 0
TextBox3.Value = 0
TextBox4.Value = 0

Label1.Font.Size = 8
Label2.Font.Size = 8
Label3.Font.Size = 8
Label4.Font.Size = 8

Label1.Caption = ""
Label2.Caption = ""
Label3.Caption = ""
Label4.Caption = ""

With ComboBox1
        .AddItem ("Circle")
        .AddItem ("Right Triangle")
End With


End Sub

Private Sub CommandButton1_Click()
Dim NumPi As Double
NumPi = Application.WorksheetFunction.Pi()

If ComboBox1.Value = "Right Triangle" Then
Label1.Caption = "Base"
Label2.Caption = "Height"
Label3.Caption = "Area"
Label4.Caption = "Third leg"

    If Not IsNumeric(TextBox1.Value) Or Not IsNumeric(TextBox2.Value) Or Not IsNumeric(TextBox3.Value) Or Not IsNumeric(TextBox4.Value) Then
        MsgBox "Enter only numbers in the boxes. Spaces make an error also."
        TextBox1.Value = 0
        TextBox2.Value = 0
        TextBox3.Value = 0
        TextBox4.Value = 0
    End If
 
    If TextBox1.Value = 0 Or TextBox2.Value = 0 Then
        MsgBox "Base and height are both essential to calculate the area and the third leg."
        TextBox1.Value = 0
        TextBox2.Value = 0
        TextBox3.Value = 0
        TextBox4.Value = 0
    End If

    If Not TextBox1.Value = 0 Or Not TextBox2.Value = 0 Then
        TextBox3.Value = CDbl(TextBox1.Value * TextBox2.Value * 0.5)
        TextBox4.Value = CDbl(Sqr(TextBox1.Value * TextBox1.Value + TextBox2.Value * TextBox2.Value))
    End If
End If

If ComboBox1.Value = "Circle" Then
    TextBox4.Value = 0
        Label1.Caption = "Radius"
        Label2.Caption = "Diameter"
        Label3.Caption = "Area"
        Label4.Caption = ""

    If Not IsNumeric(TextBox1.Value) Or Not IsNumeric(TextBox2.Value) Or Not IsNumeric(TextBox3.Value) Then
        MsgBox "Enter only numbers in the boxes. Spaces make an error also."
        TextBox1.Value = 0
        TextBox2.Value = 0
        TextBox3.Value = 0
    End If

    If Not TextBox1.Value = 0 Then
        TextBox2.Value = CDbl(TextBox1.Value * 2)
        TextBox3.Value = CDbl(TextBox1.Value * TextBox1.Value * NumPi)
    ElseIf Not TextBox2.Value = 0 Then
        TextBox1.Value = CDbl(TextBox2.Value / 2)
        TextBox3.Value = CDbl((TextBox2.Value / 2) * (TextBox2.Value / 2) * NumPi)
    ElseIf Not TextBox3.Value = 0 Then
        TextBox1.Value = CDbl(Math.Sqr(TextBox3.Value / NumPi))
        TextBox2.Value = CDbl(Math.Sqr(TextBox3.Value / NumPi) * 2)
    End If
End If


End Sub

Private Sub CommandButton2_Click()

TextBox1.Value = 0
TextBox2.Value = 0
TextBox3.Value = 0
TextBox4.Value = 0

End Sub