计算循环中的行以获得列的总和

时间:2017-03-01 15:29:52

标签: excel vba excel-vba

我需要在Col中搜索Excel中的文本更改,然后在之前对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

2 个答案:

答案 0 :(得分:0)

首先计算RowCount。也许这就是你想要的:

RowCount = iRow - 1

这意味着您要从第二行开始求和。你可能需要调整它。

然后

"=SUM(R[-" & RowCount & "]C:R[-1]C)"

答案 1 :(得分:0)

我回答了我自己的问题,需要引用和&amp;围绕RowCount

ActiveCell.FormulaR1C1 = "=SUM(R[-" & RowCount & "]C:R[-1]C)"
相关问题