sum or subtract for the same cell based on match column between two sheets

Omran Y

Board Regular
Joined
Jul 17, 2023
Messages
60
Office Version
  1. 2013
Platform
  1. Windows
Hello
I want match names in column C for EMPLOYEE sheets with column B for TT sheet if the column C in TT sheet contains Advance payment then should subtract amount for adjacent cell for the name in EMPLOYEE sheet from amount for adjacent cell for the word Advance payment
for instance OMAR ALI OMAR =-20000-2000=-22000 as show in third picture as highlighted cell in D2 and change adjacent cell for NET AMOUNT by sum amount for adjacent NAME and adjacent cell for SALARY to become -22000+3000= -19000.
if the column C in TT sheet contains Pay payment then should sum amount for adjacent cell for the name in EMPLOYEE sheet from amount for adjacent cell for the word Pay payment
for instance AHMED OMAR ALI=-2000+3000=1000 as show in last sheet as highlighted cell in D8 and change adjacent cell for NET AMOUNT by sum amount for adjacent NAME and adjacent cell for SALARY to become 1000+3000= 4000.
and when repeat the same name in TT sheet then should move to next row when add new data as in pic2 and pic3 without repeat calculation amounts have ever sum or subtract( the next file will depend on last amount for previous file) .
the result should be in EMPLOYEE sheet in column D for adjacent cells NAME & NET AMOUNT
I highlighted the changed cells in third picture for each case when change data in TT sheet .
also posted here
sum or subtract for the same cell based on match column between two sheets
case1

1.xlsm
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-20,000.00
3SALARY3,000.00
4NET AMOUNT-17,000.00
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI-2,000.00
9SALARY3,000.00
10NET AMOUNT1,000.00
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR1000
15SALARY2500
16NET AMOUNT3500
EMPLOYEES



1.xlsm
ABCD
211/02/2023OMAR ALI OMARAdvance payment by safe2,000.00
312/02/2023ALI MAHMUD OMARAdvance payment by bank2,500.00
413/02/2023AHMED OMAR ALIPay payment safe3,000.00
TT




what I want in EMPLOYEES sheet
1.xlsm
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-22000
3SALARY3000
4NET AMOUNT-19000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI1000
9SALARY3000
10NET AMOUNT4000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR-1500
15SALARY2500
16NET AMOUNT1000
EMPLOYEES
Cell Formulas
RangeFormula
D16D16=D14+D15


case2
2.xlsm
ABCD
1DATENAMEDETAILSAMOUNT
211/02/2023OMAR ALI OMARAdvance payment by safe2,000.00
312/02/2023ALI MAHMUD OMARAdvance payment by bank2,500.00
413/02/2023AHMED OMAR ALIPay payment safe3,000.00
514/03/2023OMAR ALI OMARPay payment bank5,000.00
TT


what I want in EMPLOYEES sheet based on last updating in CASE1 above
2.xlsm
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-17000
3SALARY3000
4NET AMOUNT-14000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI1000
9SALARY3000
10NET AMOUNT4000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR-1500
15SALARY2500
16NET AMOUNT1000
EMPLOYEES



case 3
3.xlsm
ABCD
1DATENAMEDETAILSAMOUNT
211/02/2023OMAR ALI OMARAdvance payment by safe2,000.00
312/02/2023ALI MAHMUD OMARAdvance payment by bank2,500.00
413/02/2023AHMED OMAR ALIPay payment safe3,000.00
514/03/2023OMAR ALI OMARPay payment bank5,000.00
614/03/2023OMAR ALI OMARPay payment bank2,000.00
715/03/2023ALI MAHMUD OMARPay payment safe3,200.00
TT


what I want in EMPLOYEES sheet based on last updating in CASE2 above
3.xlsm
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-15000
3SALARY3000
4NET AMOUNT-12000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI1000
9SALARY3000
10NET AMOUNT4000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR1700
15SALARY2500
16NET AMOUNT4200
EMPLOYEES

I hope to find answering here for this project
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this:

VBA Code:
Sub sum_or_subtract()
  Dim f As Range, c As Range
  Dim det As String
  Dim v As Double
   
  det = LCase("Advance payment")
  For Each c In Sheets("TT").Range("B2", Sheets("TT").Range("B" & Rows.Count).End(3))
    Set f = Sheets("EMPLOYEES").Range("C:C").Find(c.Value, , xlValues, xlWhole, , , False)
    If Not f Is Nothing Then
      v = c.Offset(, 2).Value * IIf(Left(LCase(c.Offset(, 1).Value), Len(det)) = det, -1, 1)
      f.Offset(0, 1).Value = f.Offset(0, 1).Value + v
      f.Offset(2, 1).Value = f.Offset(0, 1).Value + f.Offset(1, 1).Value
    End If
  Next
End Sub
 
Last edited:
Upvote 0
Hi,
I'm not sure what my bad , but the code doesn't do anything and there is no error !:unsure:
 
Upvote 0
Check the name on both sheets, they must be exactly the same, check that they do not have spaces before or after, just like your examples.
 
Upvote 0
ok I copy in new file works but not completely
I would compare result in pic 5 based on pic4 in OP with what I got based on pic 4
what I got
ورقة عمل Microsoft Excel جديد ‫(5)‬.xlsx
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-19000
3SALARY3000
4NET AMOUNT-16000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI4000
9SALARY3000
10NET AMOUNT7000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR-4000
15SALARY2500
16NET AMOUNT-1500
EMPLOYEES



the right based on op

ورقة عمل Microsoft Excel جديد ‫(5)‬.xlsx
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-17000
3SALARY3000
4NET AMOUNT-14000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI1000
9SALARY3000
10NET AMOUNT4000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR-1500
15SALARY2500
16NET AMOUNT1000
EMPLOYEES
 
Upvote 0
With this on employees sheet
varios 13ene2024b.xlsm
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-20000
3SALARY3000
4NET AMOUNT-17000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI-2000
9SALARY3000
10NET AMOUNT1000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR1000
15SALARY2500
16NET AMOUNT3500
EMPLOYEES


An this on TT sheet:
varios 13ene2024b.xlsm
ABCD
1DATENAMEDETAILSAMOUNT
211/02/2023OMAR ALI OMARAdvance payment by safe2000
312/02/2023ALI MAHMUD OMARAdvance payment by bank2500
413/02/2023AHMED OMAR ALIPay payment safe3000
514/03/2023OMAR ALI OMARPay payment bank5000
TT



The result is:
varios 13ene2024b.xlsm
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-17000
3SALARY3000
4NET AMOUNT-14000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI1000
9SALARY3000
10NET AMOUNT4000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR-1500
15SALARY2500
16NET AMOUNT1000
EMPLOYEES

1705239718396.png
The results are correct, but check the values in TT sheet.
;)
 
Upvote 0
here is again testing
ورقة عمل Microsoft Excel جديد ‫(5)‬.xlsx
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-22000
3SALARY3000
4NET AMOUNT-19000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI1000
9SALARY3000
10NET AMOUNT4000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR-1500
15SALARY2500
16NET AMOUNT1000
EMPLOYEES
Cell Formulas
RangeFormula
D16D16=D14+D15


ورقة عمل Microsoft Excel جديد ‫(5)‬.xlsx
ABCD
1DATENAMEDETAILSAMOUNT
211/02/2023OMAR ALI OMARAdvance payment by safe2000
312/02/2023ALI MAHMUD OMARAdvance payment by bank2500
413/02/2023AHMED OMAR ALIPay payment safe3000
514/03/2023OMAR ALI OMARPay payment bank5000
TT



this is what I got

ورقة عمل Microsoft Excel جديد ‫(5)‬.xlsx
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-19000
3SALARY3000
4NET AMOUNT-16000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI4000
9SALARY3000
10NET AMOUNT7000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR-4000
15SALARY2500
16NET AMOUNT-1500
EMPLOYEES


the right


ورقة عمل Microsoft Excel جديد ‫(5)‬.xlsx
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-17000
3SALARY3000
4NET AMOUNT-14000
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI1000
9SALARY3000
10NET AMOUNT4000
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR-1500
15SALARY2500
16NET AMOUNT1000
EMPLOYEES

I would see your comment.
thanks for your time .
 
Upvote 0
Why -17000?
1705269975912.png

Omar starts with -22000:
1705270115162.png

On TT sheet:
1705270047281.png

Then:
if the column C in TT sheet contains Advance payment then should subtract amount
-22000 - 2000 (Advance payment) = -24000

if the column C in TT sheet contains Pay payment then should sum amount
-24000 + 5000 (Pay payment) = -19000

The sums are according to the OP.
1705270492673.png

So what's the problem, is your original specification wrong?
 
Upvote 0
and when repeat the same name in TT sheet then should move to next row when add new data
it will ignore the old data should calculate based on last data in lastrow
Why -17000?
in this case Omar starts with -22000: and select 5000 from the same name in last row and ignore old data in second row in TT sheet to become -22000+5000=-17000
always should search for the lastrow in TT sheet when repeat the same name .
 
Upvote 0
Ok try this:

VBA Code:
Sub sum_or_subtract()
  Dim f As Range, c As Range
  Dim det As String
  Dim v As Double
  Dim lr As Long
   
  det = LCase("Advance payment")
  With Sheets("TT")
    lr = .Range("B" & Rows.Count).End(3).Row
    For Each c In .Range("B2:B" & lr)
      If WorksheetFunction.CountIf(.Range(c, .Range("B" & lr)), c.Value) = 1 Then
        Set f = Sheets("EMPLOYEES").Range("C:C").Find(c.Value, , xlValues, xlWhole, , , False)
        If Not f Is Nothing Then
          v = c.Offset(, 2).Value * IIf(Left(LCase(c.Offset(, 1).Value), Len(det)) = det, -1, 1)
          f.Offset(0, 1).Value = f.Offset(0, 1).Value + v
          f.Offset(2, 1).Value = f.Offset(0, 1).Value + f.Offset(1, 1).Value
        End If
      End If
    Next
  End With
End Sub

😊
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,051
Members
452,542
Latest member
Bricklin

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