Solved

Excel VBA  remove duplicates

Posted on 2012-03-12
7
434 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

623 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