計算判斷式可以使用的迴圈有兩種,若有確定的範圍可以使用for迴圈,若資料量較大或隨時會更新則可使用while迴圈。
![](https://tingyucho.home.blog/wp-content/uploads/2021/08/image-50.png?w=1024)
For迴圈/If 判斷式
r = 0 '預設值
For r = 2 To 8 '計算範圍
w = Cells(r, 4) '體重
l = Cells(r, 3) '身高
bmi = Math.Round(w / (l * 0.01) ^ 2) '計算BMI
Cells(r, 5) = bmi '填入
'判斷BMI
If bmi >= 35 Then
Cells(r, 7) = "重度肥胖"
ElseIf bmi >= 30 Then
Cells(r, 7) = "中度肥胖"
ElseIf bmi >= 27 Then
Cells(r, 7) = "輕度肥胖"
ElseIf bmi >= 24 Then
Cells(r, 7) = "過重"
ElseIf bmi >= 18.5 Then
Cells(r, 7) = "正常"
Else
Cells(r, 7) = "過瘦"
End If
Next r 'For迴圈結尾
Do While迴圈/ Select判斷式
On Error Resume Next '
r = 2
Do While Not IsEmpty(Cells(r, 1))
w = Cells(r, 4) '體重
l = Cells(r, 3) '身高
bmi = Math.Round(w / (l * 0.01) ^ 2) '計算BMI
Cells(r, 6) = bmi '填入
Select Case bmi
Case Is < 18.5
Cells(r, 8) = "過瘦"
Case Is < 24
Cells(r, 8) = "正常"
Case Is < 27
Cells(r, 8) = "過重"
Case Is < 30
Cells(r, 8) = "輕度肥胖"
Case Is < 35
Cells(r, 8) = "中度肥胖"
Case Else
Cells(r, 8) = "重度肥胖"
End Select
r = r + 1
Loop