Solved

Excel VBA  remove duplicates

Posted on 2012-03-12
7
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

751 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