mardi 3 décembre 2019

ElseIf Statement in VBA not applying all the conditions?

I'm new to coding and attempting to learn some VBA. I've started trying to build simple applications to help me learn, including this BMI calculator.

I would like a simple string text message to be displayed in the specified text box in accordance with the calculated BMI score and the subsequent If and Elseif statements.

If the BMI score returns less than 18, the text box displays "You Are Underweight" as desired. Similarly, if the BMI score returns a value of equal or greater than 18, "You Are The Ideal Weight" is displayed.

However, this is where the problem is encountered. If the BMI score is greater than 25, "You Are The Ideal Weight" continues to be displayed, with the additional ElseIf and Else statements seemingly just ignored?

Can anyone spot what I am missing? My code is shown here:

Option Explicit

Sub BMI()

Dim Height As Variant
Dim Weight As Variant
Dim BMI As Double

Height = Height_Input.Value
Weight = Weight_Input.Value


If IsNumeric(Height_Input.Value) And IsNumeric(Weight_Input.Value) Then

    BMI = CDbl(Weight_Input.Value) / (CDbl(Height_Input.Value) * CDbl(Height_Input.Value))

    BMI_Output.Value = BMI

Else: MsgBox ("Please Enter Numeric Values")

End If

If BMI < 18 Then
    The_Verdict.Value = "You Are Underweight"

    ElseIf 18 <= BMI < 25 Then
        The_Verdict.Value = "You Are The Ideal Weight"

        ElseIf 25 <= BMI < 30 Then
            The_Verdict.Value = "You Are OverWeight"

Else: The_Verdict.Value = "You Are Obese"

End If

If Height_Input.Value = ("") Or Weight_Input.Value = ("") Then

    The_Verdict.Value = ""

End If

End Sub

Aucun commentaire:

Enregistrer un commentaire