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

Alaa mg

Active Member
Joined
May 29, 2021
Messages
365
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
 
I am sorry this line clears the formats-
VBA Code:
wsRR.UsedRange.Offset(1, 0).Clear
you can change it to
VBA Code:
wsRR.UsedRange.Offset(1, 0).ClearContents
but that will leave you with a stray yellow highlight. I can try to modify the code to take that Yellow highlight out but I can't get to it right now...
 
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
still delete borders .
but that will leave you with a stray yellow highlight
yes I see that.
I can try to modify the code to take that Yellow highlight out but I can't get to it right now.
no problem take your time . you can do that tomorrow if it's late time in your location.;)
 
Upvote 0
Try adding this as the next line below the line referenced in my post #11
VBA Code:
.UsedRange.Offset(1, 0).Interior.Color = -4142

Try re-formatting the RR tab the way you would like and run the code again with the above line placed where indicated and the line above to read .clearcontents.

although there is nothing in the code to create new border, so that will still be an issue. I will be able to work the borders a little later, but this new line should clear the previous yellow highlight.
 
Upvote 0
How about this for a complete code:

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
    Dim hdr
    
    Application.ScreenUpdating = False
    wsN.Activate
    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
    hdr = Array("ITEM", "DETAILS", "NAMES", "AMOUNT")
    With wsRR
        .Activate
        .UsedRange.Offset(1, 0).ClearContents
        .UsedRange.Offset(1, 0).Interior.Color = -4142
        .UsedRange.HorizontalAlignment = -4108
        .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)
        .Range("A1:D1") = hdr
        .Range("A1:D1").Interior.Color = RGB(255, 255, 0)
    End With
    With wsRR.Range("A1:D" & lRow + 1).Borders
        .LineStyle = xlContinuous
    End With
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
You're welcome, I was happy to help. Thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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