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 Apr 22nd, 2002, 01:53 PM   #1
wilkisa
Board Regular
 
Join Date: Apr 2002
Location: Decatur IL, USA
Posts: 494
Default

We receive a large sheet from an outside source. It has 157 columns (A - FA), and 4041 rows.

The vendor name is in column C and the vendor address is in column D. I need a macro that will find duplicates and delete the entire row.

Example:

Col. C...Wonder Widgets...Col. D...123 Main
Col. C...Wonder Widgets...Col. D...456 Elm
Col. C...Wonder Widgets...Col. D...123 Main

I want to delete only the second occurance of Wonder Widgets at 123 Main. The other two Wonder Widgets should remain in the sheet.

Thanks in advance for any help,
Shirlene
wilkisa is offline   Reply With Quote
Old Apr 22nd, 2002, 03:21 PM   #2
Asala42
Board Regular
 
Join Date: Feb 2002
Location: Southfield,MI USA
Posts: 1,030
Default

G'day,

The Advanced Filter feature has a way to copy "unique records only" - this is a good method if you don't need a macro.

Hope that helps,
Adam
Asala42 is offline   Reply With Quote
Old Apr 23rd, 2002, 05:11 AM   #3
wilkisa
Board Regular
 
Join Date: Apr 2002
Location: Decatur IL, USA
Posts: 494
Default

I have tried the AutoFilter unique records feature but it won't work for these sheets. The problem is that the vendor who sends us the sheet has numbered each record with duplicate records having different numbers.

Any other suggestions?
wilkisa is offline   Reply With Quote
Old Apr 23rd, 2002, 05:35 AM   #4
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Are you opposed to using a VBA macros?
If not, what is the first row your data begins on?

Tom


[ This Message was edited by: TsTom on 2002-04-23 04:41 ]
Tom Schreiner is offline   Reply With Quote
Old Apr 23rd, 2002, 06:07 AM   #5
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi again.
If you need help with this please repost.
Assumes data starts on row 2...

Quote:
Sub NoDups()
Dim RowCntr As Long, FromRowCntr As Long, LastRow As Long
Dim CompareStr
Dim FirstString As String, SecondString As String
On Error Resume Next
Range("A2:FZ20000").Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
LastRow = Range("C1:C" & Range("C65536").End(xlUp).Row).Rows.Count
For RowCntr = 2 To LastRow
For FromRowCntr = RowCntr + 1 To LastRow
FirstString = Range("C" & RowCntr) & Range("D" & RowCntr)
SecondString = Range("C" & FromRowCntr) & Range("D" & FromRowCntr)
CompareStr = StrComp(FirstString, SecondString, 1)
If CompareStr = 0 Then
Rows(FromRowCntr & ":" & FromRowCntr).Delete Shift:=xlUp
FromRowCntr = FromRowCntr - 1
End If
Next
Next
End Sub
Thanks,
Tom

[ This Message was edited by: TsTom on 2002-04-23 05:09 ]
Tom Schreiner is offline   Reply With Quote
Old Apr 23rd, 2002, 08:12 AM   #6
wilkisa
Board Regular
 
Join Date: Apr 2002
Location: Decatur IL, USA
Posts: 494
Default

The macro works great on sheets that are a few hundred rows. However, it dies while working in sheets of a few thousand rows. I ran it for 20 minutes on my sheet with 4041 rows and finally found that Excel stopped responding. Any ideas?
wilkisa is offline   Reply With Quote
Old Apr 23rd, 2002, 09:38 AM   #7
Asala42
Board Regular
 
Join Date: Feb 2002
Location: Southfield,MI USA
Posts: 1,030
Default

Hi again,

I guess you could always go with a semi-manual method (a few steps but not too horrible):

1: insert a new column somewhere and on the 2nd row use the formula =C2&D2 (copy down).

The idea is to catch the criteria for a unique record - you may wish to add more cells to the concatenated to ensure a good criteria. For the example's sake I'll assume you type this formula in C2.

2. Insert a 2nd new column with the formula: =countif($C$2:C2,C2) (copy down again).

3. Copy and paste these columns as values

4. Sort by this numbered column, then manually delete any entries >1

5. (optional) resort by their initial order.

Hope that helps,
Adam
Asala42 is offline   Reply With Quote
Old Apr 23rd, 2002, 11:31 AM   #8
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
Will need a sample sheet...
Unless someone can find out why my code is doing what you said.
I'm assuming that the deletion of the rows might be sending it into an endless loop.
Maybe try this as an alternative...

Quote:
Sub NoDups()
Dim RowCntr As Long, FromRowCntr As Long, LastRow As Long
Dim CompareStr
Dim FirstString As String, SecondString As String
On Error Resume Next
Range("A2:FZ20000").Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
LastRow = Range("C1:C" & Range("C65536").End(xlUp).Row).Rows.Count
For RowCntr = 2 To LastRow
For FromRowCntr = RowCntr + 1 To LastRow
FirstString = Range("C" & RowCntr) & Range("D" & RowCntr)
SecondString = Range("C" & FromRowCntr) & Range("D" & FromRowCntr)
CompareStr = StrComp(FirstString, SecondString, 1)
If CompareStr = 0 Then
Rows(FromRowCntr & ":" & FromRowCntr).ClearContents
FromRowCntr = FromRowCntr - 1
End If
Next
Next
For RowCntr = LastRow To 2
If Range("C" & RowCntr) = "" Then _
Rows(RowCntr & ":" & RowCntr).Delete Shift:=xlUp
Next
End Sub
If that does not work and no one else provides a solution, then I will need a sample worksheet to test it out.
Tom
Tom Schreiner is offline   Reply With Quote
Old Apr 23rd, 2002, 01:07 PM   #9
Tom Schreiner
Board Regular
 
Join Date: Mar 2002
Location: Cincinnati, Ohio, USA
Posts: 6,824
Default

Hi
I failed to adjust LastRow when a row was deleted.
Hence, the forever loop...
As an added precaution, I added this line of code:
Quote:
If Len(Trim(FirstString)) = 0 Then Exit Sub
It is commented out. If you are still having problems then
make this line active in the code.
Quote:
Sub NoDups()
Dim RowCntr As Long, FromRowCntr As Long, LastRow As Long
Dim CompareStr
Dim FirstString As String, SecondString As String
On Error Resume Next
Range("A2:FZ20000").Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
LastRow = Range("C1:C" & Range("C65536").End(xlUp).Row).Rows.Count
For RowCntr = 2 To LastRow
FirstString = Range("C" & RowCntr) & Range("D" & RowCntr)
'If Len(Trim(FirstString)) = 0 Then Exit Sub
For FromRowCntr = RowCntr + 1 To LastRow
SecondString = Range("C" & FromRowCntr) & Range("D" & FromRowCntr)
CompareStr = StrComp(FirstString, SecondString, 1)
If CompareStr = 0 Then
Rows(FromRowCntr & ":" & FromRowCntr).Delete Shift:=xlUp
FromRowCntr = FromRowCntr - 1
LastRow = LastRow - 1
End If
Next
Next
End Sub
Thanks,
Tom
Tom Schreiner is offline   Reply With Quote
Old Apr 23rd, 2002, 08:28 PM   #10
Brabantio
 
Join Date: Mar 2002
Posts: 15
Default

A macro can be created with the macro recorder by recording the following steps (note : it has been assumed that only column D needs to be checked for duplicates on the basis that a particular address will not have more than one customer name - if this is not the case, post again) :-

- Insert a column before column D
- In the inserted column, select from D1 down to the last row with data in column E
- Type in the formula =IF(COUNTIF($E$1:E1,E1)>1,1,"") and press Ctrl+Enter
- Go to Edit>GoTo>Special>Formula>Numbers and click OK
- Go to Edit>Delete>EntireRow
- Delete Column D


Here's a cleaned-up version of the recorded macro :-

Sub Delete_Duplicates()
Application.ScreenUpdating=False
Columns(4).Insert
With Range([E1], [E65536].End(xlUp)).Offset(0, -1)
.FormulaR1C1 = "=IF(COUNTIF(R1C5:RC[1],RC[1])>1,1,"""")"
On Error Resume Next
.SpecialCells(xlCellTypeFormulas, 1).EntireRow.Delete
On Error GoTo 0
.EntireColumn.Delete
End With
End Sub


[ This Message was edited by: Brabantio on 2002-04-23 19:30 ]
Brabantio 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 -4. The time now is 04:37 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes