2017-03-01 108 views
0

我需要搜索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 
+2

' 「= SUM(R [」 &行數與「C:[R [-1] C)「' –

回答

0

首先計算RowCount。也許這是你想要的:

RowCount = iRow - 1 

這意味着你想總結從第二行開始。你可能需要調整它。

然後

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

我回答我自己的問題,需要報價和&周圍行數

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