Results 1 to 3 of 3

Summing based on 2 criteria matching

Threaded View

  1. #1
    Forum Contributor
    Join Date
    04-13-2011
    Location
    Havant, Hants, England
    MS-Off Ver
    Excel 2010
    Posts
    116

    Summing based on 2 criteria matching

    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
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] summing based on a criteria but the criteria is not the format i want
    By uimhirADo in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-01-2013, 04:16 PM
  2. Replies: 6
    Last Post: 06-08-2011, 09:49 PM
  3. Summing Ranges in workbook based on matching values
    By scaffdog845 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-13-2011, 02:02 PM
  4. Replies: 2
    Last Post: 06-08-2010, 12:58 PM
  5. Summing based on a Criteria
    By Smee in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 05-20-2008, 11:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1