【发布时间】:2019-12-03 14:35:10
【问题描述】:
我不熟悉编码并尝试学习一些 VBA。我已经开始尝试构建简单的应用程序来帮助我学习,包括这个 BMI 计算器。
我希望根据计算出的 BMI 分数和随后的 If 和 Elseif 语句在指定的文本框中显示一条简单的字符串文本消息。
如果 BMI 分数小于 18,则文本框会根据需要显示“您体重过轻”。同样,如果 BMI 分数返回等于或大于 18 的值,则会显示“您是理想体重”。
但是,这就是遇到问题的地方。如果 BMI 分数大于 25,则继续显示“You Are The Ideal Weight”,而附加的 ElseIf 和 Else 语句似乎只是被忽略了?
谁能发现我错过了什么?我的代码显示在这里:
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
【问题讨论】:
标签: vba if-statement