Dynamic "Columns:=Array(1,2,3)" in RemoveDuplicates excel VBA

Joined
Apr 1, 2020
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
I am new to VBA.
Currently I am using this,
lastColumn is last column of worksheet.
lastRow is last row of worksheet.

It is working fine for:

ActiveSheet.Range(Cells(1,1), Cells (lastRow,lastColumn)). RemoveDuplicates(Columns:=Array(1,2,3), Headers:=xlNo)

- But it will work only for 3 columns each time.

I want to remove duplicate rows from Excel worksheet by making Array dynamically.


MyArray=Evaluate ("Row(1:" & lastColumn & ")")
ActiveSheet.Range(Cells(1,1), Cells (lastRow,lastColumn)). RemoveDuplicates(Columns:=myArray, Headers:=xlNo)

But getting"invalid procedure call or argument" error.

Is there any possible way to do it for a header/ non-header spreadsheet?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How about
VBA Code:
   ReDim MyArray(lastcolumn - 1)
   For i = 1 To lastcolumn
      MyArray(i - 1) = i
   Next i
   ActiveSheet.Range(Cells(1, 1), Cells(LastRow, lastcolumn)).RemoveDuplicates (MyArray), xlNo
 
Upvote 0
How about
VBA Code:
   ReDim MyArray(lastcolumn - 1)
   For i = 1 To lastcolumn
      MyArray(i - 1) = i
   Next i
   ActiveSheet.Range(Cells(1, 1), Cells(LastRow, lastcolumn)).RemoveDuplicates (MyArray), xlNo
Thank you.
Also it works fine.

VBA Code:
Dim x as Variant
x = Evaluate (Columns("A":"Z"))
ReDim Preserve x(0 To lcol-1) As Variant
.Range(Cells(1,1),Cells(lrow,lcol)). RemoveDuplicates (x), xlNo
 
Last edited:
Upvote 0
VBA Code:
Sub DumpaDuplicates(RA As Range, CCC$)    ' columns parameter CCC as CSV   like  3    2,5,4    2,1
   Dim ColDoV() As Variant
   Dim SSA$(), UB&, II&
   SSA = Split(CCC, ","): UB = UBound(SSA)  ' split CCC into an string array => variant array
   If UB >= 0 Then  ' remove them
      ReDim ColDoV(UB)
      For II = 0 To UB
         ColDoV(II) = SSA(II)
      Next II
      RA.RemoveDuplicates ColDoV, xlGuess
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,631
Messages
6,173,465
Members
452,516
Latest member
archcalx

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