VBA to Sum Mulitple Lines of Data to One Line of Data

dallin01

Board Regular
Joined
Sep 16, 2009
Messages
61
Hi, I'm looking for code/examples that sums multiple lines of data in one total line of Data. Example: I would like to sum the amounts by contract into new worksheet one line for each contract. Thankyou!!

Data:
Date Contract Amount
1/1/1 11111 555
1/1/1 11111 666
1/1/1 11111 777
2/2/2 22222 888
3/3/3 33333 999
3/3/3 33333 111

New worksheet data:
date contract Amount
1/1/1 1111 1998
2/2/2 2222 777
3/3/3 3333 1110
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
did you try pivot table ?

pivot table is the best option rather then the VB code
 
Upvote 0
ok then try below code assuming your Data in Sheet name DATA

Code:
Sub Test()
On Error Resume Next
Sheets("Result").Select
If Err.Number = 9 Then Sheets.Add.Name = "Result"
Sheets("Result").UsedRange.ClearContents
Sheets("DATA").UsedRange.Copy Sheets("Result").Range("A1")
lc = Range("B" & Rows.Count).End(xlUp).Row
Range("D1").Value = "Amount"
Range(Range("D2"), Range("D" & lc)).FormulaR1C1 = "=Sumif(c2:c2,Rc[-2],c3:c3)"
Range(Range("D2"), Range("D" & lc)).Value = Range(Range("D2"), Range("D" & lc)).Value
Columns(3).Delete
ActiveSheet.Range("A:C").RemoveDuplicates Columns:=2, Header:=xlYes
Range("A1").Select
End Sub
 
Last edited:
Upvote 0
Hi, I'm looking for code/examples that sums multiple lines of data in one total line of Data. Example: I would like to sum the amounts by contract into new worksheet one line for each contract. Thankyou!!

Data:
Date Contract Amount
1/1/1 11111 555
1/1/1 11111 666
1/1/1 11111 777
2/2/2 22222 888
3/3/3 33333 999
3/3/3 33333 111

New worksheet data:
date contract Amount
1/1/1 1111 1998
2/2/2 2222 777
3/3/3 3333 1110

Will your data ever have more than one date for the same contract?
 
Upvote 0
Yes, however, I was hoping for some code examples to expand my vba horizons.
OK.

Here's another code example.

Change in DATAsheet Column3 automatically adjusts sum in Result sheet.
Code:
Sub contracts_sum()
Dim a, b, c, q(), i
Sheets("DATA").Activate
a = Range("B1", Cells(Rows.Count, "b").End(3)(2)).Value
ReDim q(1 To UBound(a, 1))
Cells(1).Resize(, 3).Copy Sheets("Result").Cells(1)
b = 2
For i = 3 To UBound(a, 1)
       If a(i, 1) <> a(b, 1) Then
        c = c + 1
        Cells(b, 1).Resize(, 2).Copy Sheets("Result").Cells(c + 1, 1)
        q(c) = Cells(b, 3).Resize(i - b).Address
        b = i
    End If
Next i
For i = 1 To c
    Sheets("Result").Cells(i + 1, 3) = "=sum(DATA!" & q(i) & ")"
Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,622
Messages
6,167,137
Members
452,098
Latest member
xel003

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top