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?
 

Some videos you may like

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,231
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,126,940
Messages
5,621,732
Members
415,853
Latest member
Newlife72

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