Delete customer if contains the same amount otherwise just subtract between two sheets

Alaa mg

Active Member
Joined
May 29, 2021
Messages
375
Office Version
  1. 2019
Hi
I want deleting any name contains the same amount in NAMES sheet after comparison with BAD DEBTS sheet.
should match column C in NAMES sheet with column D in BAD DEBTS and search for the same amount in AMOUNT column then should delete the whole row for the customer , if doesn't contain the same amount in NAMES sheet after comparison with BAD DEBTS sheet then just subtract amount in NAMES sheet from BAD DEBTS sheet( be careful will repeat customers in BAD DEBTS so should merge amount for duplicate customer before delete the row if equal the same amount or subtraction.also should re autonumbering in column A and sum the AMOUNT column.
the result should be in PR.
I would macro to deal about big data for each sheet.
BAD DEBTS.xlsm
ABCD
1ITEMDETAILSNAMESAMOUNT
21OPENING BALANCE 08/01/22Ala1,200.00
32OPENING BALANCE 11/01/22Aln3,000.00
43OPENING BALANCE 17/01/22Alm1,500.00
54OPENING BALANCE 17/01/22Alaa1,200.00
65OPENING BALANCE 17/01/22Allia1,000.00
76OPENING BALANCE 17/01/22Aliaa1,100.00
87OPENING BALANCE 17/01/22Anila1,500.00
98OPENING BALANCE 17/01/22Aali1,000.00
109OPENING BALANCE 17/01/22Aml1,100.00
11TOTAL12,600.00
NAMES


BAD DEBTS.xlsm
ABCDE
1ITEMDATEINVOICE NONAMESAMOUNT
2101/01/2024INV AL-00Ala1,200.00
3202/01/2024INV AL-01Aln500.00
4303/01/2024INV AL-02Aln200.00
5404/01/2024INV AL-03Alaa200.00
6505/01/2024INV AL-04Allia200.00
7606/01/2024INV AL-05Allia800.00
BAD DEBTS


result
BAD DEBTS.xlsm
ABCD
1ITEMDETAILSNAMESAMOUNT
21OPENING BALANCE 11/01/22Aln2,300.00
32OPENING BALANCE 17/01/22Alm1,500.00
43OPENING BALANCE 17/01/22Alaa1,000.00
54OPENING BALANCE 17/01/22Aliaa1,100.00
65OPENING BALANCE 17/01/22Anila1,500.00
76OPENING BALANCE 17/01/22Aali1,000.00
87OPENING BALANCE 17/01/22Aml1,100.00
9TOTAL9,500.00
RR
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
How about this, it tested and works with your given data as shown.

VBA Code:
Sub Sum_Values2()

    Dim wsN As Worksheet: Set wsN = Worksheets("Names")
    Dim wsBD As Worksheet: Set wsBD = Worksheets("BAD DEBTS")
    Dim wsRR As Worksheet: Set wsRR = Worksheets("RR")
    Dim dic As Object: Set dic = CreateObject("Scripting.Dictionary")
    Dim a, arr, arr2, arr3, nams, rng As Range
    Dim r As Long, c As Long, i As Long, ct As Long, x As Long, d As String, lRow As Long
    
    Application.ScreenUpdating = False
    a = wsBD.Range("D2:E" & Cells(Rows.Count, 4).End(xlUp).Row)
    nams = wsN.Range("A2:D" & Cells(Rows.Count, 3).End(xlUp).Row)
    ct = 1
    For i = 1 To UBound(a)
          dic(a(i, 1)) = dic(a(i, 1)) + a(i, 2)
    Next

    ReDim arr3(1 To dic.Count, 1 To 2)
    ReDim res(1 To UBound(nams), 1 To 4)

    arr = dic.keys
    arr2 = dic.items
    For r = 1 To UBound(arr) + 1
        arr3(r, 1) = arr(r - 1)
        arr3(r, 2) = arr2(r - 1)
    Next

    For c = 1 To UBound(nams)
        For x = 1 To UBound(arr3)
            If nams(c, 3) = arr3(x, 1) Then
                    nams(c, 4) = nams(c, 4) - arr3(x, 2)
                    If nams(c, 4) = 0 Then nams(c, 4) = ""
            End If
        Next
    Next
    
    With wsRR
        .Activate
        .UsedRange.Offset(1, 0).Clear
        .Range("A2").Resize(UBound(nams, 1), UBound(nams, 2)) = nams
        lRow = wsRR.Cells(Rows.Count, 3).End(xlUp).Row
        .Range("A2").Formula = "=ROW()-1"
        .Range("A2").AutoFill wsRR.Range("A2:A" & lRow)
        .Range("D2:D" & lRow).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        lRow = wsRR.Cells(Rows.Count, 3).End(xlUp).Row
        .Range("A" & lRow + 1) = "TOTAL"
        Set rng = wsRR.Range("D2:D" & lRow)
        d = rng.Address(False, False)
        x = WorksheetFunction.Sum(Range(d))
        .Range("D" & lRow + 1) = x
        .Range("A" & lRow + 1 & ":D" & lRow + 1).Interior.Color = RGB(255, 255, 0)
    End With
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Thanks
it shows mismatch error in this line
VBA Code:
nams(c, 4) = nams(c, 4) - arr3(x, 2)
this case when there is no data in RR sheet when try run macro from first time.
 
Upvote 0
I run the code with the RR sheet being blank and it works fine. There is of course no header row written but it is otherwise populated correctly.
 
Upvote 0
Make sure you are run it with the Names tab active. If you run it with the RR tab active and it is blank, it will throw the error you described... You can add a line of code at the beginning to activate the Names tab to avoid this issue.
 
Upvote 0
Make sure you are run it with the Names tab active. If you run it with the RR tab active and it is blank, it will throw the error you described... You can add a line of code at the beginning to activate the Names tab to avoid this issue.
yes this is what I did it.
 
Upvote 0
And is the code working correctly?
 
Upvote 0
And is the code working correctly?
not completely , there is no formatting and borders for headers or numberformat .
BAD DEBTS.xlsm
ABCD
1
21OPENING BALANCE 11/01/22Aln2300
32OPENING BALANCE 17/01/22Alm1500
43OPENING BALANCE 17/01/22Alaa1000
54OPENING BALANCE 17/01/22Aliaa1100
65OPENING BALANCE 17/01/22Anila1500
76OPENING BALANCE 17/01/22Aali1000
87OPENING BALANCE 17/01/22Aml1100
9TOTAL9500
RR
Cell Formulas
RangeFormula
A2:A8A2=ROW()-1

you can see my result in OP.
 
Upvote 0
I was working under the assumption that the sheet would have had the formatting already applied and that the code would fill the values. Formatting is the easy stuff. Do you need additional code that will write the Header Row...
 
Upvote 0
it's not problem for me I do manually , but the problem your code delete number format(#,##0.00) also delete borders when I do manually .
do you have any idea to fix it?
 
Upvote 0

Forum statistics

Threads
1,223,952
Messages
6,175,595
Members
452,657
Latest member
giadungthienduyen

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