VBA Code for a Function to Delete Duplicate Rows based on Multiple Column Criteria

OilEconomist

Active Member
Joined
Dec 26, 2016
Messages
439
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance for your assistance

I am attempting to create a function which will delete duplicate rows based on multiple column criteria. Three issues are occurring.

(1) Delete Rows - How do I delete the rows as the rows are being cleared versus deleted.


(2) Column Array Dynamic - I want to check if they have duplicate values can vary, so I am using an Array. Keep in mind I would like the Array to be dynamic so I can use something like Array(1, 4, 5) in some instances or Array(1) in other instances. Right now, it gives me an error in the line of code within the function where the issue is "Columns:=Ary"

VBA Code:
RngRmv.RemoveDuplicates Columns:=Ary, Header:=xlYes

When I change it to the following, it works: RngRmv.RemoveDuplicates Columns:=Array(1, 4, 5), Header:=xlYes


(3) Header - Be able to specify is it has a header or not through the function, but I get an error on the following code where the issue is "Header:=Hrd"

VBA Code:
RngRmv.RemoveDuplicates Columns:=Array(1, 4, 5), Header:=Hrd

When I change it to the following, it works:
VBA Code:
RngRmv.RemoveDuplicates Columns:=Array(1, 4, 5), Header:=xlYes



VBA Code:
Sub TestRemoveDuplicatesF()

Dim ShtNm As String
ShtNm = ActiveSheet.name

Dim RowStart As Long
RowStart = 9

Dim RowEnd As Long
RowEnd = 19

Dim Ary() As Variant 'an array
Ary = Array(4, 5)

Dim Hdr As String
Hdr = "xlYes"
Dim RemoveDuplicates As Variant

RemoveDuplicates = RemoveDuplicatesF(ShtNm, RowStart, RowEnd, Ary, Hdr)


End Sub


'*************************************************************************************************************

Function RemoveDuplicatesF(ShtNm As String, RowStart As Long, RowEnd As Long, Ary As Variant, Hdr As String) As Variant

    Dim RngRmv As Range

    Set RngRmv = ActiveSheet.Range(RowStart & ":" & RowEnd)
    
    RngRmv.ClearOutline
    RngRmv.RemoveDuplicates Columns:=Ary, Header:=Hdr
    
End Function






The following is the fu
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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