Calculate difference between 2 records in a group

Anna Deux

New Member
Joined
Jul 22, 2004
Messages
22
I have a big spreadsheet of over 3000 records grouped by Bin then by Item ID that I import from a text document.
Each Item ID group will not have the same number of records and might have 100 records more or less. In all the rows that show "Item Total", in column F, I have to calculate the difference between the last record and the first record of each Item ID group (cells that I highlighted in green).
The result that I should get in cell F8 should be 84 (207-123=84), and 45 in cell F16, and 39 in F21.
Will you please help me with this? Any help would be greatly appreciated?
Anh Tu1.xls
ABCDEF
1BinItemIDDateInQtyValue
2
3001
4001.14-Aug88.4123
56-Aug87.8156
68-Aug88.3189
710-Aug67.8207
8ItemTotal3032.3F7-F4=84
9
10BinTotal3032.3
11
12
13002
14002.16-Aug1313.3205
157-Aug911.2250
16ItemTotal2224.5F15-F14=45
17
18002.29-Aug55.8111
1910-Aug79.2124
2011-Aug88.3150
21ItemTotal2023.3F20-F18=39
22
23BinTotal4447.8
24
25GRANDTOTAL7480.1
Anh Tu
 
Hi again Anna,
How about we run another loop to check for any value of less than or equal to zero?
Try replacing what you've got now with this.
Code:
Sub Test1()
Application.ScreenUpdating = False
j = Range("F65536").End(xlUp).Row

For i = j To 1 Step -1
If Cells(i, 6) <> "" And Cells(i + 1, 6) = "" Then Cells(i, 6)(2, 1) = Cells(i, 6) - Cells(i, 6).End(xlUp)
Next i

For i = 1 To j
If Not IsEmpty(Cells(i, 6)) And Cells(i, 6) <= 0 Then Cells(i, 6) = 0
Next i

Application.ScreenUpdating = True
End Sub
This any better?
Dan
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Hello Dan:

I modified the code but it does not work completely. There are several rows that gave wrong results. How can I post back so you can have a look what's wrong?

As always thanks a lot for your help

Anna
 
Upvote 0
I run the code that Half Ace modified but there are still some errors. Will you please help me solve it. There are errors where results should be negative numbers or 0. Your help would be very appreciated!

Bin Item ID Date In Qty Value

001
001.1 4-Aug 8 8.4 123
6-Aug 8 7.8 156
8-Aug 8 8.3 189
10-Aug 6 7.8 100
Item Total 30 32.3 0 Should be -13

001.2 4-Aug 7 9.2 200
Item Total 7 9.2 100 OK


Bin Total 37 41.5


002
002.1 6-Aug 13 13.3 205
7-Aug 9 11.2 250
Item Total 22 24.5 45 OK

002.2 9-Aug 5 5.8 111
Item Total 5 5.8 0 OK

Bin Total 27 30.3


003 003.1 9-Aug 5 5.8 111
10-Aug 7 9.2 124
11-Aug 8 8.3 150
12-Aug 6 10.5 160.0
Item Total 26 33.8 49.0 OK

003.2 9-Aug 5 5.8 123.0
Item Total 5 5.8 0.0 OK

003.3 10-Aug 7 9.2 111.0
11-Aug 8 8.3 125.0
12-Aug 6 10.5 101.0
Item Total 21 28.0 0.0 Wrong, must be -10

Bin Total 52 67.6


004 004.1 9-Aug 7 9.2 123.0
Item Total 7 9.2 22.0 wrong, must be 0

004.2 2-Aug 6 8.8 236.0
11-Aug 8 8.3 245.0
12-Aug 6 10.5 226.0
Item Total 20 27.6 -10.0 OK

Bin Total 27 36.8


Errors are on row 8 (should be -13), row 40 (should be -10), and row 46 (should be 0, - whenever an item ID has only 1 record, the result should be 0).
 
Upvote 0
Sorry, I copied the spreadsheet again with HTML. Any help would be appreciated.
Test1.xls
ABCDEFG
1BinItemIDDateInQtyValue
2001
3001.14-Aug88.4123
46-Aug87.8156
5ItemTotal3032.333OK
6
7001.24-Aug79.2200
8ItemTotal79.244Shouldbe0
9
10
11BinTotal3741.5
12
13
14002
15002.16-Aug1313.3205
167-Aug911.2250
17ItemTotal2224.545OK
18
19002.29-Aug55.8111
20ItemTotal55.80OK
21
22BinTotal2730.3
23
24
25003003.19-Aug55.8111
2610-Aug79.2100
27ItemTotal1215.00.0Shouldbe-11
28
29003.29-Aug55.8123.0
3023.0Shouldbe0
Anh Tu
 
Upvote 0

Forum statistics

Threads
1,215,873
Messages
6,127,442
Members
449,382
Latest member
DonnaRisso

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