Both solutions work, but SUMIF killed my performance with large datasets. I switched to SUMIFS with a helper column - just concatenate the name with a running counter for each occurrence, then use that for calculations. For smaller sheets, Pete’s IF formula is cleaner. I still use it for anything under 500 rows. Either way, make your range references absolute for the starting point but relative for the ending point. Otherwise you’ll get wrong totals when copying the formula down.
Here’s a formula that checks if the current name matches the previous row. Put this in column E starting from your first data row: =IF(B2=B1,E1+C2-D2,C2-D2). If the names match, it adds the current lend-return calculation to the previous balance. If they don’t match, it starts fresh with just lend minus return. Just adjust the row numbers to match where your data starts. I’ve used similar formulas for project budgets - they work great once you get the setup right.
try =sumif($b$2:b2,b2,$c$2:c2)-sumif($b$2:b2,b2,$d$2:d2) in e2 and drag down. this sums all lend/return amounts for each name up to the current row - gives you running totals that reset when new names show up. way better than if statements for bigger datasets.