LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 63
Default Help to modify a macro for deleting rows based on two or more colu

Hello

I have a macro I have been using to delete duplicate rows which contain the
same value in two columns. This works well, as long as I manually sort Column
A in ascending order first, before running the macro. The code is as follows:

--------------------------

Sub DeleteDuplicates2Columns()
Dim LR As Long, i As Long
Application.ScreenUpdating = False

LR = Range("A" & Rows.Count).End(xlUp).Row

For i = LR To 2 Step -1
If Cells(i, "A") = Cells(i - 1, "A") And _
Cells(i, "L") = Cells(i - 1, "L") Then _
Rows(i).Delete xlShiftUp
Next i

Application.ScreenUpdating = True
End Sub

--------------------------

As you can see, the above code removes duplicate rows if they contain
matching data in columns A and L. In the past if I have needed to change this
to remove duplicate rows based on matching values in different columns I just
modified the code. However, I now want to share this with other users who
have no idea about code, so my preference would be to have an input box where
they can just select the columns.

Also, in the past I have just added another line of code if I wanted the
macro to do the comparisons across three columns. For example:

For i = LR To 2 Step -1
If Cells(i, "A") = Cells(i - 1, "A") And _
Cells(i, "L") = Cells(i - 1, "L") Then _
Rows(i).Delete xlShiftUp
Next i

BECOMES

For i = LR To 2 Step -1
If Cells(i, "A") = Cells(i - 1, "A") And _
Cells(i, "K") = Cells(i - 1, "K") And _
Cells(i, "L") = Cells(i - 1, "L") Then _
Rows(i).Delete xlShiftUp
Next i

So, is there any way to do this in a user-friendly manner for other users?
For instance, using an input box to specify how many columns the user wants
to interrogate and then having the macro display enough input boxes for the
user to actually specify the columns. Perhaps this could work if the number
of columns were restricted to a maximum of 5?

Just out of curiosity, can someone explain why I have to sort Column A into
ascending order first before running the macro? Is this macro limited in some
way so that it only compares the rows one at a time adjacent to one another?
If so, how could I modify the macro so that this isn't the case? Perhaps it
would be easier to just get the macro to sort Column A first before doing
anything else.

Sorry for the long post. Hope this all makes sense. Any help would be
greatly appreciated!

Thanks!

Joe.
--
If you can measure it, you can improve it!
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting Rows with a macro based on a date value Max2073 Excel Worksheet Functions 2 September 7th 09 10:28 PM
modify macro to execute based on current path intoit Excel Programming 6 July 20th 09 08:09 AM
Chart changing based on change in data source (number of rows/colu Bal Ram Bhui, Jakarta Charts and Charting in Excel 1 November 16th 08 06:02 AM
Can you hide and unhide rows with a macro based on content of colu Husker87 Excel Programming 4 September 24th 08 01:06 AM
Deleting rows based on data NOT meeting criteria --working macro here, just need help with tweaking Zarlot531 Excel Programming 4 April 29th 07 12:40 AM


All times are GMT +1. The time now is 07:10 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"