sum or subtract for the same cell based on match DATE

Omran Y

Board Regular
Joined
Jul 17, 2023
Messages
60
Office Version
  1. 2013
Platform
  1. Windows
Hello
I want matching names in column C for EMPLOYEE sheets with column B for TT sheet based on DATE (TODAY) if the column C in TT sheet contains Advance payment and the column A is DATE in TT sheet , then should subtract amount for adjacent cell for the name in EMPLOYEE sheet from amount for adjacent cell for the word Advance payment, if the column C in TT sheet contains Pay payment and the column A is DATE in TT sheet then should sum amount for adjacent cell for the name in EMPLOYEE sheet from amount for adjacent cell for the word Pay payment, and when repeat the same name in TT sheet and repeat the date today in column A then should get the mount from the last row contains date ,name when calculation by sum or subtract based on column C in TT sheet for two words(Advance payment,Pay payment).
also change adjacent cell for NET AMOUNT by sum amount for adjacent NAME and adjacent cell for SALARY in EMPLOYEES
ORIGINAL DATA for EMPLOYEES ,TT sheets


OU.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 OMAR1,000.00
15SALARY2,500.00
16NET AMOUNT3,500.00
EMPLOYEES


OU.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
615/03/2023OMAR ALI OMARAdvance payment by safe3,000.00
716/03/2023OMAR ALI OMARPay payment bank1,200.00
817/03/2023ALI MAHMUD OMARAdvance payment by bank8,000.00
917/03/2023OMAR ALI OMARPay payment bank2,000.00
1017/03/2023OMAR ALI OMARAdvance payment by safe3,000.00
1117/03/2023AHMED OMAR ALIPay payment bank3,000.00
1217/03/2023ALI MAHMUD OMARPay payment bank3,000.00
TT


so when brings the amount from TT sheet should brings from the last row for the repeat name in column C and repeate DATE from column A as highlighted by red , as to yellow there is no repeated name .


OU.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
615/03/2023OMAR ALI OMARAdvance payment by safe3,000.00
716/03/2023OMAR ALI OMARPay payment bank1,200.00
817/03/2023ALI MAHMUD OMARAdvance payment by bank8,000.00
917/03/2023OMAR ALI OMARPay payment bank2,000.00
1017/03/2023OMAR ALI OMARAdvance payment by safe3,000.00
1117/03/2023AHMED OMAR ALIPay payment bank3,000.00
1217/03/2023ALI MAHMUD OMARPay payment bank3,000.00
TT


the result in EMPLOYEES sheet should be as highlighted by red which cells should change them .

OU.xlsm
ABCD
1START DATELAST DATENAMEFIRST BALANCE
201/01/202331/01/2023OMAR ALI OMAR-23,000.00
3SALARY3,000.00
4NET AMOUNT-20,000.00
5
6
7START DATELAST DATENAMEFIRST BALANCE
801/01/202331/01/2023AHMED OMAR ALI1,000.00
9SALARY3,000.00
10NET AMOUNT4,000.00
11
12
13START DATELAST DATENAMEFIRST BALANCE
1401/01/202331/01/2023ALI MAHMUD OMAR4,000.00
15SALARY2,500.00
16NET AMOUNT6,500.00
EMPLOYEES




by the way I got this code by Dante's assistance , but I want adjusting or alternative
sum or subtract for the same cell based on match column between two sheets
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
thanks
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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