VBA to remove duplicates on multiple criteria

tourertt

New Member
Joined
Sep 4, 2014
Messages
11
Hello All,New member from Australia here and I have a question. I have tried search, but didn't find a topic addressing specifically what i need. I have a spreadsheet with Excel table in it with the list of invoices for the month.Date Customer Code Customer Name Amount1/09/2014 CUST01 Customer 1 1001/09/2014 CUST02 Customer 2 1501/09/2014 CUST03 Customer 3 2001/09/2014 CUST01 Customer 1 -1002/09/2014 CUST02 Customer 2 2503/09/2014 CUST03 Customer 3 -2004/09/2014 CUST04 Customer 4 300What i need is a VBA macro that will remove invoices and credits for the same customer, done on the same day.So, if Date matches, customer code matches, and amount is opposite ($100 invoice and -$100 credit note as in example above) i need to remove rows with both the invoice and the credit.Please help me with a code for it.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
tourertt,

How was the fishing??

I had checked yesterday, and, early today, the USGS 01426500 WEST BRANCH DELAWARE RIVER AT HALE EDDY NY water depth height = 2.7 feet, but by the time I got there (tons of road upgrades) the water was too deep, 3.0+ feet, for me to do any wading/fly fishing with my bad leg. But the day was clear, low humidity, and, low temperature. I was just great to be out in the fresh air.


New sample raw data:


Excel 2007
ABCDEFG
1Robayne Pty Ltd6314
2NDF Daily Debtors Finance Report
3Created:05 SEP 2014 05:23PM
4Start Date:Friday, 5 September 2014
5End Date:Friday, 5 September 2014
6Date Used:Date Processed
7Age Group(s):AD, B, BURN, DA, HEN, HO, LA, M, OP
8
9KEY1KEY2CUSTOMERNOCUSTOMERNAMEINVOICENODATEINVOICEAMOUNT
10DEBTFINANCE18PICK01PICKEN AUTO BODY REPAIR CENTRE (P,CS,K)CN282325-Sep-14($1,042.80)
11DEBTFINANCE21DYNA03DYNAMIC SIGNS & ENGRAVING (ES)CN282355-Sep-14($682.00)
12DEBTFINANCE23PALM01Palmerston Smash RepairsCN282375-Sep-14($272.39)
13DEBTFINANCE22HIGH03HIGHWAY SMASH REPAIRS P/LCN282365-Sep-14($207.58)
14DEBTFINANCE26DTHI00DT HILOAD AUSTRALIA PTY LTDCN282415-Sep-14($191.07)
15DEBTFINANCE24SGIT00S-Git- Automotive Repairs (P)CN282385-Sep-14($77.57)
16DEBTFINANCE17BODY02BODYCRAFT COLLISION REPAIRSCN282315-Sep-14($31.20)
17DEBTFINANCE19ROCK01ROCKINGHAM SMASH REPAIRSCN282335-Sep-14($22.67)
18DEBTFINANCE20ROCK01ROCKINGHAM SMASH REPAIRSCN282345-Sep-14($22.67)
19DEBTFINANCE25PALM01Palmerston Smash RepairsCN282405-Sep-14($7.39)
20DEBTFINANCE106ALPH02ALPHABLAST HENDERSONIV8890595-Sep-14$4.11
21DEBTFINANCE67PICK01PICKEN AUTO BODY REPAIR CENTRE (P,CS,K)IV8889525-Sep-14$10.43
22DEBTFINANCE94ROCK01ROCKINGHAM SMASH REPAIRSIV8890175-Sep-14$22.67
23
Sheet1


After the new macro:


Excel 2007
ABCDEFG
1Robayne Pty Ltd6314
2NDF Daily Debtors Finance Report
3Created:05 SEP 2014 05:23PM
4Start Date:Friday, 5 September 2014
5End Date:Friday, 5 September 2014
6Date Used:Date Processed
7Age Group(s):AD, B, BURN, DA, HEN, HO, LA, M, OP
8
9KEY1KEY2CUSTOMERNOCUSTOMERNAMEINVOICENODATEINVOICEAMOUNT
10DEBTFINANCE106ALPH02ALPHABLAST HENDERSONIV8890595-Sep-14$4.11
11DEBTFINANCE17BODY02BODYCRAFT COLLISION REPAIRSCN282315-Sep-14($31.20)
12DEBTFINANCE26DTHI00DT HILOAD AUSTRALIA PTY LTDCN282415-Sep-14($191.07)
13DEBTFINANCE21DYNA03DYNAMIC SIGNS & ENGRAVING (ES)CN282355-Sep-14($682.00)
14DEBTFINANCE22HIGH03HIGHWAY SMASH REPAIRS P/LCN282365-Sep-14($207.58)
15DEBTFINANCE23PALM01Palmerston Smash RepairsCN282375-Sep-14($272.39)
16DEBTFINANCE25PALM01Palmerston Smash RepairsCN282405-Sep-14($7.39)
17DEBTFINANCE18PICK01PICKEN AUTO BODY REPAIR CENTRE (P,CS,K)CN282325-Sep-14($1,042.80)
18DEBTFINANCE67PICK01PICKEN AUTO BODY REPAIR CENTRE (P,CS,K)IV8889525-Sep-14$10.43
19DEBTFINANCE19ROCK01ROCKINGHAM SMASH REPAIRSCN282335-Sep-14($22.67)
20DEBTFINANCE24SGIT00S-Git- Automotive Repairs (P)CN282385-Sep-14($77.57)
21
22
23
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub RemoveDupes_V3()
' hiker95, 09/08/2014, ME803452
Dim r As Long, lr As Long, rr As Long, n As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 3).End(xlUp).Row
Columns(8).ClearContents
With Range("H10:H" & lr)
  .FormulaR1C1 = "=RC[-2]&RC[-5]&RC[-4]"
  .Value = .Value
End With
Range("A10:H" & lr).Sort key1:=Range("H10"), order1:=1
For r = 10 To lr
  n = Application.CountIf(Columns(8), Cells(r, 8).Value)
  If n = 2 Then
    If Cells(r, 8).Value = Cells(r + 1, 8).Value = 0 Then
      Range("A" & r & ":H" & r + 1).ClearContents
    End If
  ElseIf n > 2 Then
    For rr = r To r + n - 1
      If Cells(rr, 7).Value + Cells(rr + 1, 7).Value = 0 Then
        Range("A" & rr & ":H" & rr + 1).ClearContents
      End If
    Next rr
  End If
  r = r + n - 1
Next r
Range("A10:H" & lr).SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
lr = Cells(Rows.Count, 1).End(xlUp).Row
Range("A10:H" & lr).Sort key1:=Range("C10"), order1:=1, key2:=Range("F10"), order2:=1
Columns(8).ClearContents
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the RemoveDupes_V3 macro.
 
Last edited:
Upvote 0
Good day outside is what we all need sometimes :)
The code in your last post worked well, but it struggled to remove dupes in situation like below
Excel Workbook
ABCDEFG
9KEY1KEY2CUSTOMERNOCUSTOMERNAMEINVOICENODATEINVOICEAMOUNT
12DEBTFINANCE5AUTO09AUTO CARE PANEL WORKS (CS)CN2825509 Sep 2014-$249.87
124DEBTFINANCE53AUTO09AUTO CARE PANEL WORKS (CS)IV88949109 Sep 2014$249.87
125DEBTFINANCE67AUTO09AUTO CARE PANEL WORKS (CS)IV88950909 Sep 2014$249.87
198Total$249.87
DEBTFINANCE
 
Upvote 0
tourertt,

We have gone thru three versions of the macro, because, you keep changing what the dataset looks like.

Now you introduce a new dataset with Subtotals?????


I have far exceeded the normal amount of time I allocate for solving problems/requests from web sites like MrExcel.


Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0

Forum statistics

Threads
1,224,803
Messages
6,181,055
Members
453,014
Latest member
Chris258

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