当范围基于另一列中的文本时,列中行数未知的小计公式

我有一个从单元格I11开始的小计列,但我不知道行数,因为它会每次都会改变。 在列的最后是基于小计的乘法函数。

我正在尝试创build一个macros,它将计算从I11开始的行数,直到列F中的值等于“工作完成的值”

这是我迄今为止:

Sub Total_Amount() 'New_Entry Macro Dim PPC1 As Worksheet Dim rowNo As Integer rowNo = ActiveCell.Row If PPC1.Range("F11:F").Value = "VALUE OF WORK DONE" Then With Range("I" & Rows.Count).End(xlUp) .Offset(1).Formula = "=SUM(I11:" & .Address(0, 0) & ")" End With End If End Sub 

答案仍然出现在列的末尾,而不是旁边的“完成工作的价值”

  Sub Total_Amount() 'New_Entry Macro Dim ws As Excel.Worksheet Dim lRow As Long Dim lLast As Long Set ws = Worksheets("PPC 1") Row = 11 Do While lRow <= ws.UsedRange.Rows.Count If ws.Range("F" & lRow).Value = "Value of Work Done" Then lLast = lRow Exit Do End If lRow = lRow + 1 Loop ws.Range("I" & lRow + 1).Formula = Application.Sum("I11:I" & lLast) End Sub 

 Dim ws as excel.worksheet Dim lRow As Long Dim lLast as Long Set ws = Worksheets("PPC 1") lRow = 11 Do While lRow <= ws.UsedRange.Rows.count If ws.Range("F" & lRow).Value = "Value of Work Done" then lLast = lRow Exit do End if lRow = lRow + 1 Loop 

然后把公式写到你想要的任何列上

 ws.Range("I" & lLast).Formula = Application.Sum("I11:I" & lLast)