循环遍历行并将单元格值存储为variables以运行fomrulas,最终导致另一个单元格

高强 我想扫描每一行,并拉出某些单元格并将它们存储为variables。 当我到达行的末尾时,我想运行一系列的公式和vlookups。 我有公式和vlookups都设置,但我需要扫描每一行,然后把这些公式的结果,并把它们放回另一个单元格。 这里是示例代码

Dim height As Double Dim weight As Double Dim gender As String Dim newWeight as Double 'I'd like to set the loop here 'for each row do this height = d2.value weight = f2.value gender = k2.value 'then run through my equations If gender = "F" and weight < 20 Then newWeight = weight + 5 ElseIf gender = "F" and weight > 20 Then newWeight = weight -2 End If l2.value = newWeight.value 'Then clear variables and run through the new row 

我的方程稍微复杂一点,但我认为这会让我开始。

**编辑:我需要引用我正在使用的工作表吗? 我敢打赌,我这样做

我不会惹你的代码,但这应该做你所需要的,

 Dim height As Double Dim weight As Double Dim gender As String Dim newWeight As Double Dim lastrow As Long lastrow = Cells(Rows.Count, 1).End(xlUp).row For i = 2 To lastrow 'I'd like to set the loop here 'for each row do this height = CDbl(Range("D" & i).value) weight = CDbl(Range("F" & i).value) gender = Range("K" & i).value 'then run through my equations If gender = "F" And weight < 20 Then newWeight = weight + 5 ElseIf gender = "F" And weight > 20 Then newWeight = weight - 2 End If Range("I" & i).value = newWeight 'Then clear variables and run through the new row newWeight = 0 height = 0 weight = 0 gender = 0 Next 

看下面的代码

 Sub temp() Dim height As Double Dim weight As Double Dim gender As String Dim newWeight As Double Dim lastRow As Integer lastRow = Range("C" & Rows.Count).End(xlUp).Row For i = 2 To lastRow 'I'd like to set the loop here 'for each row do this height = Range("D" & i).Value weight = Range("F" & i).Value gender = Range("K" & i).Value 'then run through my equations If gender = "F" Then If weight < 20 Then newWeight = weight + 5 ElseIf weight > 20 Then newWeight = weight - 2 End If End If Range("L" & i).Value = newWeight 'Then clear variables and run through the new row Next i End Sub 

你还需要弄清楚如果weight = 20会发生什么:P