Hi, I've created a routine that loops down through a spreadsheet and, if the date (column 1) and the Cheque No (column 4) are equal, sums the values of the individual transactions in a new column (called "Transaction totals"). The routine works but I'm sure it's a little longwinded.... Does anyone know a slicker way to approach this? I've added a sample file from the larger application with the relevant sheet and code in it,
thanks for any help or guidance,
regards, neil
Public Sub AddTransactionTotals()
Dim finalrow As Integer
Dim finalcol As Integer
Dim DTchqNo As Variant
Dim chqVal As Variant
Dim DTchqNo1 As Variant
Dim chqVal1 As Variant
Dim i As Integer
Dim j As Integer
Dim k As Integer
Columns("N:N").Select
Selection.Delete Shift:=xlToLeft 'Delete Transactions Totals column so code runs okay
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
finalcol = Cells(2, Columns.Count).End(xlToLeft).Column 'Will be the "Grand Total" column
Cells(2, finalcol + 1).Value = "Transaction Totals" 'Put in title for column
For i = 3 To finalrow - 1 'Overall loop to work down through the data
DTchqNo = Cells(i, 1).Value & Cells(i, 4).Value 'set initial value
chqVal = Cells(i, finalcol).Value 'set initial value in "Transactions total" column8
k = 0 'variable that is incremented where common cheque numbers occur to adjust i for start of next loop
j = i + 1 'variable to pick up values of next cheque number
DTchqNo1 = Cells(j, 1).Value & Cells(j, 4).Value 'set value of variable for next cheque number
Do Until DTchqNo1 <> DTchqNo 'Loop thro' next cheque numbers if there are still equal
chqVal1 = Cells(j, finalcol).Value 'set value of variable
chqVal = chqVal + chqVal1 'increase value of cheque value for common cheques
j = j + 1
DTchqNo1 = Cells(j, 1).Value & Cells(j, 4).Value 'pick up value of next cheque number to test Do loop again
k = k + 1
Loop
Cells(i, finalcol + 1).Value = chqVal 'Once no more of same cheques put value into first line of cheque number
i = i + k 'Increase value of i to take account of number of same cheques so it starts on new line
Next i
End Sub
Bookmarks