Solved

Excel VBA  remove duplicates

Posted on 2012-03-12
7
423 Views
Last Modified: 2012-11-20
Hi All,

I have a bit of code to remove duplicates as follows:

 ActiveSheet.Range(Cells(1, 1), Cells(lastrow, 25)).RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5), Header:=xlYes

This works fine but sometime there are not 5 columns of data so the macro breaks.

Following this I was wondering, assuming I have ColNo as the variable representing the number of columns in the array, what I would have as the code?
0
Comment
Question by:matt_m
7 Comments
 
LVL 6

Expert Comment

by:reitzen
ID: 37713274
If the columns actually are not there when empty, you could use the Excel function COUNTA to get the number of columns and either loop to add the column numbers to a string and then use the VBA function SPLIT to move them to an array.
0
 
LVL 9

Expert Comment

by:suvmitra
ID: 37713304
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 37714155
Try this

ActiveSheet.Range(Cells(1, 1), Cells(lastrow, 25)).RemoveDuplicates Columns:=Evaluate("=column(A1:" & Cells(1, 5).Address & ")"), Header:=xlYes
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 38616189
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38616190
http://37714155 is an answer and will suffice as a solution. The number 5 in the formula may be replaced with a variable as requested.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel VBA User Form Help 21 29
Excel conditional formatting based on 'zero value' 6 20
Clear Filter 8 40
Why do my Excel files become huge? 27 34
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question