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

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
56,904
Office Version
  1. 365
Platform
  1. Windows
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
 
Joined
Apr 1, 2020
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
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:
Joined
Apr 1, 2020
Messages
7
Office Version
  1. 2016
Platform
  1. Windows
Than
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 Fluff.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,195
Messages
5,640,793
Members
417,166
Latest member
Funwayo

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
Top