If I understand your problem correctly you have a sheet with accounts and amounts and you would like to get the sum by account.
The first image here represents my two column Account/amount sheet which I've kept named as "sheet1" the second image represents the end result.
This is the vba code I used to come up with the solution. I've kept comments in the code to help you see how I accomplish what I believe you are trying to do.
Sub test()
Dim CountSaved1 As Long
Dim sum As Long
Dim ws As Worksheet
sum = 0
'sort by account first.
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A4:B50")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' number of rows we will be summing per account.
CountSaved1 = Range("B5").End(xlDown).Row 'WorksheetFunction.Count(Worksheets("Sheet1").Range("B5:B50")) 'Counting the number of filled cells for the loop
For Value1 = 5 To CountSaved1
'get the current cell and compare to the next cell.
Debug.Print (Cells(Value1, 1).Value)
currentAccount = Cells(Value1, 1).Value 'current account cell
nextAccount = Cells(Value1 + 1, 1).Value 'next account cell
priorAccount = Cells(Value1 - 1, 1).Value 'prior account cell
Debug.Print (Cells(Value1 + 1, 1).Value)
'If the current account equals the next account than sum the values
If (currentAccount = nextAccount) Then
sum = Cells(Value1 + 1, 2).Value + sum
ElseIf (currentAccount <> nextAccount) And (currentAccount = priorAccount) Then
sum = Cells(Value1 - 1, 2).Value + sum
cellAddress = getBlankCell("Cash flow Statement")
Worksheets("Cash flow Statement").Range(cellAddress) = currentAccount
Worksheets("Cash flow Statement").Range(cellAddress).Offset(0, 1) = sum
' reset sum .
sum = 0
End If
'range, criteria, sum range
'Worksheets("Cash flow Statement").Range("B4") = WorksheetFunction.SumIf(Worksheets("BS").Range("A:A"), Worksheets("Sheet1").Range("B" & Value1), Worksheets("BS").Range("G:G"))
Next Value1
End Sub
Function getBlankCell(sheet As String) As String
Dim ws As Worksheet
Dim foundCell As String
Set ws = Worksheets(sheet)
For Each cell In ws.Columns(1).Cells
If Len(cell) = 0 Then getBlankCell = cell.Address(): Exit For
Next cell
End Function
Long
notDouble
! – Pᴇʜ