MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 30th, 2004, 02:11 PM   #1
Excel-ent
 
Join Date: Mar 2004
Location: Dubai
Posts: 338
Default Duplicates - Amend Code

Dear Experts,

I am trying to remove duplicates from Column C. The Range is from C11 - C55. I have the following code, which seems to work perfect when the data starts from A1…. Could you please amend this code or give another code so that my purpose may get fulfilled.


Private Sub CommandButton1_Click()

'Add extra Column, "A" becomes "B"
Columns(1).EntireColumn.Insert

'Filter out duplicates and copy unique list to "A"
Range("B1", Range("B65536").End(xlUp)).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:=Range("A1"), Unique:=True

'Add extra Column, "B" becomes "A"
Columns(2).EntireColumn.Delete

End Sub


Regards

Shan
Excel-ent is offline   Reply With Quote
Old Mar 30th, 2004, 02:40 PM   #2
DRJ
MrExcel MVP
 
DRJ's Avatar
 
Join Date: Feb 2002
Location: California
Posts: 3,857
Default

Change to this:

Range("B11", Range("B65536").End(xlUp)).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:=Range("A11"), Unique:=True
__________________
Excel VBA Training and Certification (Lesson 1 is free)
<hr>

<hr>-Jacob
DRJ is offline   Reply With Quote
Old Mar 30th, 2004, 02:51 PM   #3
Excel-ent
 
Join Date: Mar 2004
Location: Dubai
Posts: 338
Default Re: Duplicates - Amend Code

Thank you for your reply,

The revision that you have given me again works perfect if the data is in A column, but refuses to work while the data is in C column. My data is in C Column. The column next to it i.e. B & D are not blank, they are filled with other datas. Could you please look at it once again.



Regards,

S h a n
Excel-ent is offline   Reply With Quote
Old Mar 30th, 2004, 03:09 PM   #4
bolo
 
Join Date: Mar 2002
Posts: 292
Default Re: Duplicates - Amend Code

Private Sub CommandButton1_Click()

'Add extra Column, "C" becomes "D"
Columns(3).EntireColumn.Insert

'Filter out duplicates and copy unique list to "C"
Range("D11", Range("D65536").End(xlUp)).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:=Range("C11"), Unique:=True

'Delete extra Column, "D" becomes "C"
Columns(4).EntireColumn.Delete

End Sub


It is fairly simple to work out. You should try working it out sometimes. The column insert causes column c to become column D. so your sort should be on column D. It then copies the results to column C starting from row 11.

HTH
bolo is offline   Reply With Quote
Old Mar 30th, 2004, 03:48 PM   #5
Excel-ent
 
Join Date: Mar 2004
Location: Dubai
Posts: 338
Default Re: Duplicates - Amend Code

The code does what is required. but then, why does it reduces the Column width, the original Column width is 24 in Column C, which is the format in all the other sheets as well. After applying the Code, it reduced the Column Width to 8.43.

Can this remain as it is.

Regards,

Shan
Excel-ent is offline   Reply With Quote
Old Mar 30th, 2004, 03:51 PM   #6
DRJ
MrExcel MVP
 
DRJ's Avatar
 
Join Date: Feb 2002
Location: California
Posts: 3,857
Default

Private Sub CommandButton1_Click()

'Add extra Column, "C" becomes "D"
Columns(3).EntireColumn.Insert

'Filter out duplicates and copy unique list to "C"
Range("D11", Range("D65536").End(xlUp)).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:=Range("C11"), Unique:=True

'Delete extra Column, "D" becomes "C"
Columns(4).EntireColumn.Delete

Columns("C:C").ColumnWidth = 24

End Sub
__________________
Excel VBA Training and Certification (Lesson 1 is free)
<hr>

<hr>-Jacob
DRJ is offline   Reply With Quote
Old Mar 30th, 2004, 04:00 PM   #7
bolo
 
Join Date: Mar 2002
Posts: 292
Default Re: Duplicates - Amend Code

It is probably doing that becuase whn you inserted a column you only inserted a column with the default width. Not sure where you can change the size of the defaults though. DRJ's extra line will do it for you though
bolo is offline   Reply With Quote
Old Apr 4th, 2004, 03:07 PM   #8
Excel-ent
 
Join Date: Mar 2004
Location: Dubai
Posts: 338
Default Re: Duplicates - Amend Code

Hi,

DRJ's Code does the job well ! but why does it delete / remove the contents / heading C9 and C10 whereas filtering starts from C11 .....

Regards,

Shan
Excel-ent is offline   Reply With Quote
Old Apr 4th, 2004, 06:59 PM   #9
bolo
 
Join Date: Mar 2002
Posts: 292
Default Re: Duplicates - Amend Code

The reason why C9 and c10 are deleted is is because the whole column is deleted. What this macro does is:

Step 1: insert an extra column (column C) to work in.
Step 2: filter out the duplicates and copies the results to column C, starting at C11. Note that any headings will not have been copied over.
Step 3: Delete the column of duplicates, also removing the headings.
Step 4: fix the column width

TO keep the headings, assuming they are in C9 and C10 try this:

Private Sub CommandButton1_Click()

'Add extra Column, "C" becomes "D"
Columns(3).EntireColumn.Insert

'Filter out duplicates and copy unique list to "C"
Range("D11", Range("D65536").End(xlUp)).AdvancedFilter _
Action:=xlFilterCopy, CopyToRange:=Range("C11"), Unique:=True

'Copies over the headings from column D.
Range("C9").value = Range("D9").value
Range("C10").value = Range("D10").value

'Delete extra Column, "D" becomes "C"
Columns(4).EntireColumn.Delete

Columns("C:C").ColumnWidth = 24

End Sub

HTH
bolo is offline   Reply With Quote
Old Apr 5th, 2004, 06:31 AM   #10
Excel-ent
 
Join Date: Mar 2004
Location: Dubai
Posts: 338
Default Re: Duplicates - Amend Code

Hi,

The code do not disturb the Heading NOW. but it remove the fill from the headings which is in yellow colour to White ?

Why after the code is executed, the filtered data remains of the Original fill i.e. light green and other cells (which are duplicates) below changed their fill to light yellow ?


(am i asking 2 much or 3 much) ?

Regards,

Shan
Excel-ent is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 05:42 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.