Page 435 - Computer Software Application TP - Volume 1
P. 435
COMPUTER SOFTWARE APPLICATION - CITS
Explanation:
This VBA subroutine named Calculate_BMI calculates the Body Mass Index (BMI) based on the weight and
height entered by the user and categorizes the BMI into different weight categories such as underweight, normal
weight, overweight, and obese. Here’s an explanation of the code:
1 Variable Declarations:
• weight, height, bmi: These variables of type Double are used to store the weight, height, and calculated
BMI, respectively.
• result: This variable of type String holds the category of BMI.
2 Prompt for Weight and Height:
• The user is prompted to enter their weight in kilograms and height in meters using the InputBox function.
3 Check for Valid Input:
• The code checks if the entered weight and height are greater than 0.
4 Calculate BMI:
• If the weight and height are valid, the BMI is calculated using the formula weight / (height * height).
5 Determine BMI Category:
• The BMI is categorized based on standard BMI ranges:
• BMI less than 18.5 is considered “Underweight”.
• BMI between 18.5 and 24.9 is considered “Normal weight”.
• BMI between 25 and 29.9 is considered “Overweight”.
• BMI 30 or greater is considered “Obese”.
6 Display Results:
• The calculated BMI, rounded to two decimal places using the Round function, along with its corresponding
category, is displayed in a message box using the MsgBox function.
7 Error Handling:
• If the user enters invalid input (weight or height less than or equal to 0), an error message is displayed
using MsgBox.
This subroutine provides a simple way to calculate BMI and categorize it, offering users insights into their weight
status.
420
CITS : IT & ITES - Computer Software Application - Exercise 70