Forum Discussion
Guram_Bregadze
Apr 16, 2022Copper Contributor
Macros code to remove duplicates but keep the last activity entry based on the date.
Hello, I need some solution to remove duplicates but keep those which were entered with different date. Currently I am using two macros. First, to copy data from another workbook and add it to...
Craig Hatmaker
Apr 17, 2022Iron Contributor
It sounds like you have some complex reasoning for when to remove and when not to remove duplicates, like remove all duplicates except if the duplicate is the last entry. Whatever the reasoning, I'd consider adding a helper column to identify through a formula if the entry is eligible for removal, then apply filtering over the first two columns as well as the helper column.
Suggestion: VBA is simpler with tables. It eliminates the xlUp step.
Dim oLo As ListObject
Set oLo = [A1].ListObject
oLo.DataBodyRange.RemoveDuplicates Array(1, 2)
For future consideration: Power Query can accommodate your complex reasoning and produce a filtered table (inside your current workbook or in another workbook) without removing rows from the original table. No VBA required.
In the example below.
"Table1" is the name of the Excel table. Change that name to whatever your table's name is.
"A", "B", "C" are the column heading names, so replace them with your column heading names.
"C" is the helper column. So after we complete filtering, we need to remove it.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
RemoveDups = Table.Distinct(Source, {"A","B","C"}),
RemoveCols = Table.RemoveColumns(RemoveDups,{"C"})
in
RemoveCols
Suggestion: VBA is simpler with tables. It eliminates the xlUp step.
Dim oLo As ListObject
Set oLo = [A1].ListObject
oLo.DataBodyRange.RemoveDuplicates Array(1, 2)
For future consideration: Power Query can accommodate your complex reasoning and produce a filtered table (inside your current workbook or in another workbook) without removing rows from the original table. No VBA required.
In the example below.
"Table1" is the name of the Excel table. Change that name to whatever your table's name is.
"A", "B", "C" are the column heading names, so replace them with your column heading names.
"C" is the helper column. So after we complete filtering, we need to remove it.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
RemoveDups = Table.Distinct(Source, {"A","B","C"}),
RemoveCols = Table.RemoveColumns(RemoveDups,{"C"})
in
RemoveCols
- Guram_BregadzeApr 17, 2022Copper ContributorThank you Craig,
I will try.
Kind regards,
Guram