PC Review


Reply
Thread Tools Rate Thread

Delete rows with duplicate information

 
 
Ixtreme
Guest
Posts: n/a
 
      17th Aug 2007
I have a sheet on which I import data from an external source. After
importing data, I need something to remove complete rows that match a
specific criteria.

For example:

I have in Column D a Date and in column E an EmployeeName and in
column F and OrderNumber.

after my import, I need some vba that runs through my data quickly to
remove all existing rows that have the same Date, the same
EmployeeName and an empty OrderNumber.

For example
D E F
row 50) 17-08-2007 Mark 12345
row 60) 17-08-2007 Mark
row 62) 17-08-2007 John

In this case, only row 60 should be removed

Thank you very much

 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      17th Aug 2007
Sub Removed_Duplicates()

LastRow = Cells(Rows.Count, "D").End(xlUp).Row

Remove = False
LoopCounter = 1
Do While LoopCounter <= LastRow
If IsEmpty(Cells(LoopCounter, "F")) Then

MyDate = Cells(LoopCounter, "D").Value
Employee = Cells(LoopCounter, "E").Value

For RowCount = 1 To LastRow
If RowCount <> LoopCounter Then

If (Cells(RowCount, "D").Value = MyDate) And _
(Cells(RowCount, "E").Value = Employee) Then

Remove = True
End If

End If

Next RowCount

End If

If Remove = True Then
Rows(LoopCounter).Delete
Remove = False
Else
LoopCounter = LoopCounter + 1
End If
Loop
End Sub

"Ixtreme" wrote:

> I have a sheet on which I import data from an external source. After
> importing data, I need something to remove complete rows that match a
> specific criteria.
>
> For example:
>
> I have in Column D a Date and in column E an EmployeeName and in
> column F and OrderNumber.
>
> after my import, I need some vba that runs through my data quickly to
> remove all existing rows that have the same Date, the same
> EmployeeName and an empty OrderNumber.
>
> For example
> D E F
> row 50) 17-08-2007 Mark 12345
> row 60) 17-08-2007 Mark
> row 62) 17-08-2007 John
>
> In this case, only row 60 should be removed
>
> Thank you very much
>
>

 
Reply With Quote
 
Ixtreme
Guest
Posts: n/a
 
      17th Aug 2007
Thanks Joel,

the code wors fine, however it takes more than 5 minutes to complete
this code on a 4,000 rows sheet. Is there a possibility to speed
things up?

Is it possible to do only the deleting in the selected rows for
instance? That would already help me a lot.




 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      17th Aug 2007
I made a minor correction that will probably speed up the time by 1/2. I
think this is the best that I can do. I added an Exit for statement that
will help.


Sub Removed_Duplicates()

LastRow = Cells(Rows.Count, "D").End(xlUp).Row

Remove = False
LoopCounter = inputbox("Enter Row to Start Deleteing : ")
Do While LoopCounter <= LastRow
If IsEmpty(Cells(LoopCounter, "F")) Then

MyDate = Cells(LoopCounter, "D").Value
Employee = Cells(LoopCounter, "E").Value

For RowCount = 1 To LastRow
If RowCount <> LoopCounter Then

If (Cells(RowCount, "D").Value = MyDate) And _
(Cells(RowCount, "E").Value = Employee) Then

Remove = True
exit for
End If

End If

Next RowCount

End If

If Remove = True Then
Rows(LoopCounter).Delete
Remove = False
Else
LoopCounter = LoopCounter + 1
End If
Loop
End Sub


"Ixtreme" wrote:

> Thanks Joel,
>
> the code wors fine, however it takes more than 5 minutes to complete
> this code on a 4,000 rows sheet. Is there a possibility to speed
> things up?
>
> Is it possible to do only the deleting in the selected rows for
> instance? That would already help me a lot.
>
>
>
>
>

 
Reply With Quote
 
Ixtreme
Guest
Posts: n/a
 
      18th Aug 2007
Joel,

Thanks again, but it is still not fast enough. Would it be possible to
do the deleteing only on the selected rows that are displayed after
using criteria in an autofilter?

I have an autofilter with column B displaying the specific weeknumber
of the date in column D. If the user selects for example only to
display week 33, would it be possible to run the Macro
Removed_Duplicates only for the selected rows of week 33?

Thanks for your help!

Mark


 
Reply With Quote
 
=?Utf-8?B?Sm9lbA==?=
Guest
Posts: n/a
 
      18th Aug 2007
I'm am curious if you ran the macro a 2nd time after the duplicates are
removed how fast does the macro run. The lenght of time the program takes to
run may just be a problem the first time you run the code. Onces most of the
duplicates are run the code should run quicker.

I have taken a Graduate Computer Science course on algorithms whre a major
portion of the course dealt with calculating speed of different algorithms.
I also know different methods of programming in VBA and which methods execute
faster than others.

I want to help solve your problem. The real problem with this code is doing
the delete. The faster methods won't work because the delete screws up the
way excel counts row numbers. Rows get skipped if the faster method is used.

I can change the code to check only certain row numbers. Pretty easy. But
it may result that duplicates in other rows may not get deleted.

If you only add new rows of data to previous list, I think the best method
is to leave the code alone. The code will take a long time to run the first
time you use it. But after most of the rows with no order numbers are
removed, the code speeds up.

The code checks only the rows with no order numbers. If you have 1000 rows
with now orde numbers the code may take 5 minutes to run. After the
duplicates are removed you may have only 20 rows with no order numbers. The
code should then run 1000/20 = 50 time faster. Five minutes = 600 seconds
/50 = 12 seconds. 12 seconds seems a reasonable amount of time to make sure
all your duplicates are removed.

"Ixtreme" wrote:

> Joel,
>
> Thanks again, but it is still not fast enough. Would it be possible to
> do the deleteing only on the selected rows that are displayed after
> using criteria in an autofilter?
>
> I have an autofilter with column B displaying the specific weeknumber
> of the date in column D. If the user selects for example only to
> display week 33, would it be possible to run the Macro
> Removed_Duplicates only for the selected rows of week 33?
>
> Thanks for your help!
>
> Mark
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Evaluate duplicate rows and delete information in specified columns S Himmelrich Microsoft Excel Programming 1 11th Sep 2008 06:31 AM
filtering out rows with duplicate information =?Utf-8?B?cmZJUFM=?= Microsoft Excel Misc 1 6th Mar 2007 07:51 PM
merging information from partial duplicate rows =?Utf-8?B?VG9kZA==?= Microsoft Excel Misc 3 25th Aug 2006 10:02 PM
how to delete rows based on duplicate information in a column albertpinto Microsoft Excel Discussion 5 29th May 2006 02:20 PM
Duplicate rows of information =?Utf-8?B?U2Vhbg==?= Microsoft Access Reports 1 24th Feb 2005 08:01 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:11 PM.