PDA

View Full Version : Solved: Loop Through cells and sum



viomman
11-02-2009, 01:45 PM
Ok... I am at a mental block. I grouped and sorted my cells one is a project number and the other is the dates. I want to loop through the cells and insert a summary cell. Please break my menat block and give some direction. Here is my code so far
Sub GrpSrt()
Dim LastRow As Integer 'This is the LAST Non Empty Row
LastRow = ActiveSheet.UsedRange.Row - 1 + _
ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("V2:V" & LastRow & "") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=Range("R2:R" & LastRow & "") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=Range( _
"AB2:A" & LastRow & ""), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:A" & LastRow & "")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin

End With
End Sub: pray2:

JP2112
11-02-2009, 02:03 PM
What do you mean by "summary cell" ? Do you mean a count, a sum, something else ?

viomman
11-02-2009, 02:04 PM
I

viomman
11-02-2009, 02:05 PM
I mean that I am trying to look for the end of the grouping say by date. and insert a sum cell for the amount rlated to it.

JP2112
11-02-2009, 02:09 PM
How about

ActiveSheet.Range("V" & Rows.Count).End(xlUp).Offset(1,0).Formula = "=SUM("V2:V" & LastRow & ")"

viomman
11-03-2009, 02:25 PM
Just for every ones Information I am posting my final code
Sub GrpSrt()
Dim LastRow As Integer 'This is the LAST Non Empty Row
LastRow = ActiveSheet.UsedRange.Row - 1 + _
ActiveSheet.UsedRange.Rows.Count
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("V2:V" & LastRow & "") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=Range("R2:R" & LastRow & "") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=Range( _
"AB2:A" & LastRow & ""), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:A" & LastRow & "")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
End With
Range("V2").Select
Selection.Subtotal GroupBy:=22, Function:=xlSum, TotalList:=Array(34), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
End Sub