Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel 2010 Copy Duplicates to a new worksheet

Posted on 2014-03-17
12
Medium Priority
?
3,393 Views
Last Modified: 2014-03-17
Could an expert provide me with VBA to move duplicates please.

I have a spreadsheet that has a header row in row 1

columns go from A to AJ

Number of rows will change

I need to look in column H for any duplicates and wherever I find one I need to move one of the items [complete row] to a new worksheet. [Named Duplicates] I also need the header row from Sheet1 copied onto the sheet named Duplicates.

Many thanks
0
Comment
Question by:Jagwarman
[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
  • 6
  • 3
  • 3
12 Comments
 
LVL 31

Expert Comment

by:gowflow
ID: 39933991
Question:

You say:
I need to look in column H for any duplicates and wherever I find one I need to move one of the items [complete row] to a new worksheet.

This means that only the data in Col H need to be the same to consider the whole row as Duplicate ? or we need to test the whole columns (A to AJ) to see if they are all identical then consider the row as Duplicate ??

gowflow
0
 

Author Comment

by:Jagwarman
ID: 39934016
Hi Gowflow

yes only the data in H needs to be a duplicate and then move the entire row

Regards
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39934019
Can we sort the worksheet first ?
gowflow
0
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!

 

Author Comment

by:Jagwarman
ID: 39934061
If it has to sorted then yes that would not be a problem
0
 
LVL 31

Accepted Solution

by:
gowflow earned 1800 total points
ID: 39934070
ok here it is and I sorted the worksheet (if we need not to sort then will have to change the logic).


Sub MoveDuplicates()
Dim WS As Worksheet
Dim WSDup As Worksheet
Dim MaxRow As Long, I As Long, J As Long, lCount As Long
Dim ItemColH As String

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .DisplayAlerts = False
End With

Set WS = ActiveSheet
MaxRow = WS.Range("A" & WS.Rows.Count).End(xlUp).Row

Set WSDup = Sheets("Duplicates")
J = WSDup.Range("A" & WSDup.Rows.Count).End(xlUp).Row
If J <> 1 Then J = J + 1

'---> Sort Initial sheet by Col H
WS.UsedRange.Sort key1:=WS.Columns("H"), order1:=xlAscending, Header:=xlYes, MatchCase:=False

If J = 1 Then
    '---> Copy Header
    WS.Range("1:1").Copy WSDup.Range("A1")
    J = J + 1
End If

For I = MaxRow To 2 Step -1
    If ItemColH = WS.Range("H" & I) Then
        WS.Range("A" & I).EntireRow.Copy WSDup.Range("A" & J)
        WS.Range("A" & I).EntireRow.Delete
        J = J + 1
        lCount = lCount + 1
    Else
        ItemColH = WS.Range("H" & I)
    End If
Next I

With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .DisplayAlerts = True
End With

MsgBox ("A total of " & lCount & " Dupplicate records were found and moved to sheet duplicates.")

End Sub

Open in new window



This code will move the duplicates in Col H from the Active sheet to a sheet called Duplicates after the last existing row. ie if the sheet Duplicates already contain data it will insert the new duplicates found after the last existing row. If it contains nothing it will copy the header and move the duplicates. I created a duplicate of sheet1 called Sheet1 (backup) so you can run it on sheet1 and check if all is ok.

gowflow
MoveDuplicates.xlsm
0
 
LVL 33

Assisted Solution

by:Rob Henson
Rob Henson earned 200 total points
ID: 39934173
In a separate column put the following formula, starting at row 2, note the $s:

=COUNTIF($H$1:$H2,$H2)

Copy down the extent of the data.

You can then filter on this column and those with duplicates will have values greater than 1, the first occurence will be 1 and subsequent will be greater.

When filtered, you can then copy the rows to the other sheet, select as a block but only visible rows will be selected; you will see the "marching ants" around the multiple areas.

If you then want rid of these duplicayes on original sheet, select as a block again and delete rows. There will be a message to confirm delete rows. Again only the visible rows will be deleted. Remove/Clear the filter and you will see only those with 1 entry.

Thanks
Rob H
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39934190
Robhenson

First line of the question clearly says:

Could an expert provide me with VBA to move duplicates please.


gowflow
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 39934226
Indeed he did!!

Doesn't mean to say that he won't be interested in a non-VBA answer.

Rob H
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39934231
True was just highlighting the fact.
gowflow
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 39934244
Without wishing to disrespect the question answer, I always assume that if a question is asked specifying that they need VBA; unless it is fairly obviously a repetitive routine that they are trying to automate, then they have unsuccessfully tried other avenues first and are assuming that VBA holds their answer.

My suggestion is that they may not have tried all other avenues.

Likewise in this case an Advanced Filter may work for them, reversing the logic and separating out the Unique values to another sheet.

Thanks
Rob H
0
 
LVL 31

Expert Comment

by:gowflow
ID: 39934291
or

Unless it is in fact a repettitve routine, which I usually tend to think of at first.

At the end each one sees it from his own perspective whereas I do favor VBA as not too much into formulas ... and presume you are just the opposite !!

Point of views !
gowflow
0
 

Author Closing Comment

by:Jagwarman
ID: 39934429
Gowflow did provide the solution I was looking for but robhenson also provided a solution that works.
0

Featured Post

Technology Partners: 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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

715 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