Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save erajanraja24/dfe65c2a34bb9d5ff93c6da87c301d92 to your computer and use it in GitHub Desktop.
Save erajanraja24/dfe65c2a34bb9d5ff93c6da87c301d92 to your computer and use it in GitHub Desktop.
VBA to remove duplicates in an unknown range and unknown/dynamic columns
Sub check()
'Determing the last column
LastCol = ThisWorkbook.Sheets(1).UsedRange.Columns.Count
'Determining the last row
LastRow = ThisWorkbook.Sheets(1).UsedRange.Rows.Count
'Determining the Range
Set Rng = ThisWorkbook.Sheets(1).Range(Cells(2, 1), Cells(LastRow, LastCol))
'find the number of columns and enter into the array
ReDim varArray(LastCol - 1)
'ReDim varArray1(Application.WorksheetFunction.CountA(rng))
Index = 0
'loop through range and load values to the array
Do Until Index > LastCol - 1
varArray(Index) = Index + 1
Index = Index + 1
Loop
'remove duplicates
Rng.RemoveDuplicates Columns:=(varArray), Header:=xlNo
End Sub
@David-stacks
Copy link

It worked.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment