在循环中计算列的总和
问题描述:
我需要搜索Excel中Col G中文本的更改,然后将3 Col中的值相加。到目前为止,这个工作除了总和对x行是静态的。我需要它在“RowCount”上是动态的,任何帮助都会很棒。我有一两天的时间。在循环中计算列的总和
Dim iRow As Integer, Tags As Integer
Dim oRng As Range
Dim RowCount As Integer
Set oRng = Range("G2")
iRow = oRng.Row
Tags = oRng.Column
Do
'
If Cells(iRow + 1, Tags) <> Cells(iRow, Tags) Then
Cells(iRow + 1, Tags).EntireRow.Insert Shift:=xlDown
Cells(iRow + 1, Tags).Interior.Color = 65535
Cells(iRow + 1, Tags - 1).Interior.Color = 65535
Cells(iRow + 1, Tags - 2).Interior.Color = 65535
Cells(iRow + 1, Tags - 3).Interior.Color = 65535
Cells(iRow + 1, Tags - 4).Interior.Color = 65535
Cells(iRow + 1, Tags - 5).Interior.Color = 65535
Cells(iRow + 1, Tags - 6).Interior.Color = 65535
Cells(iRow + 1, Tags).Value = Trim(Cells(iRow, Tags - 6) & " " & (Cells(iRow, Tags)) & " Totals")
Cells(iRow + 1, Tags - 6).Value = Array("Totals")
Cells(iRow + 1, Tags - 1).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-40]C:R[-1]C)" <<<<<<<<< the -40 I want to be the Integer of “RowCount”
Cells(iRow + 1, Tags - 2).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-40]C:R[-1]C)" <<<<<<<<< the -40 I want to be the Integer of “RowCount”
Cells(iRow + 1, Tags - 3).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-40]C:R[-1]C)" <<<<<<<<< the -40 I want to be the Integer of “RowCount”
iRow = iRow + 2
RowCount = 0
Else
iRow = iRow + 1
RowCount = RowCount + 1
End If
答
首先计算RowCount。也许这是你想要的:
RowCount = iRow - 1
这意味着你想总结从第二行开始。你可能需要调整它。
然后
"=SUM(R[-" & RowCount & "]C:R[-1]C)"
答
我回答我自己的问题,需要报价和&周围行数
ActiveCell.FormulaR1C1 = "=SUM(R[-" & RowCount & "]C:R[-1]C)"
' “= SUM(R [” &行数与“C:[R [-1] C)“' –