Creating a BMI Calculator

[Back to Excel VBA Sample Code]

Body Mass Index (BMI) is so popular today that it has become a standard measure for our health status. If your BMI is too high, it means you are overweight and would likely face  a host of  potential health problems associated with high BMI, such as hypertension, heart disease, diabetics and many others. We have programmed a BMI calculator using Visual Basic 6 before, but now we shall show you how to create an Excel VBA BMI calculator in MS Excel. The code is as shown below:

 Private Sub CommandButton1_Click() Dim weight, height, bmi, x As Single weight = Cells(2, 2) height = Cells(3, 2) bmi = (weight) / height ^ 2 Cells(4, 2) = Round(bmi, 1) If bmi <= 15 Then Cells(5, 2) = "Under weight" ElseIf bmi > 15 And bmi <= 25 Then Cells(5, 2) = "Optimum weight" Else Cells(5, 2) = "Over weight" End If End Sub

Explanation:

The formula for calculating BMI is

BMI=wieght(/(height2)

The function Round is to round the value to a certain decimal places. It takes the format Round(x,n), where n is the number to be rounded and n is the number of decimal places.

The second part of the program uses the If...Then..Else statement to evaluate the weight level. The output is as shown in Figure 13.1 Figure 13.1