我花了几个小时研究最简单的代码,只需要输入一个人的身高和体重,将两者分别转换成米和千克,然后计算人的BMI。我已经完成了转换,逻辑简单而且有效,但是我无法指定一系列单元格并且不断出现此错误。
Option Explicit
Const KgRate As Double = 0.45359237 'number of kg in one pound
Const PoundsInStone As Integer = 14 'number of pounds in one stone
Const InchesInFeet As Integer = 12 'number of inches in one foot
Const CmsInInch As Double = 2.54 'number of centimetres in an inch
Sub calc_BMI()
'convert height in inches to metres
Range("Sheet1!C7") = ((Range("Sheet1!C4") * InchesInFeet * CmsInInch) + (Range("Sheet1!C5") * CmsInInch)) / 100
'convert stones and pounds to kilograms
Sheets("Sheet1").Range("E10:E") = ((Sheets("Sheet1").Range("C10:C") * PoundsInStone * KgRate) + (Sheets("Sheet1").Range("D10:D") * KgRate))
End Sub
答案 0 :(得分:0)
也许:
Option Explicit
Const KgRate As Double = 0.45359237 'number of kg in one pound
Const PoundsInStone As Integer = 14 'number of pounds in one stone
Const InchesInFeet As Integer = 12 'number of inches in one foot
Const CmsInInch As Double = 2.54 'number of centimetres in an inch
Sub calc_BMI()
Dim N As Long, i As Long
With Sheets("Sheet1")
N = .Cells(Rows.Count, "C").End(xlUp).Row
'convert height in inches to metres
.Range("C7") = ((.Range("C4") * InchesInFeet * CmsInInch) + (.Range("C5") * CmsInInch)) / 100
'convert stones and pounds to kilograms
For i = 10 To N
.Range("E" & i) = ((.Range("C" & i) * PoundsInStone * KgRate) + (.Range("D" & i) * KgRate))
Next i
End With
End Sub