Solved

Excel 2010 Copy Duplicates to a new worksheet

Posted on 2014-03-17
12
2,713 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
  • 6
  • 3
  • 3
12 Comments
 
LVL 29

Expert Comment

by:gowflow
Comment Utility
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
Comment Utility
Hi Gowflow

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

Regards
0
 
LVL 29

Expert Comment

by:gowflow
Comment Utility
Can we sort the worksheet first ?
gowflow
0
 

Author Comment

by:Jagwarman
Comment Utility
If it has to sorted then yes that would not be a problem
0
 
LVL 29

Accepted Solution

by:
gowflow earned 450 total points
Comment Utility
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 31

Assisted Solution

by:Rob Henson
Rob Henson earned 50 total points
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 29

Expert Comment

by:gowflow
Comment Utility
Robhenson

First line of the question clearly says:

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


gowflow
0
 
LVL 31

Expert Comment

by:Rob Henson
Comment Utility
Indeed he did!!

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

Rob H
0
 
LVL 29

Expert Comment

by:gowflow
Comment Utility
True was just highlighting the fact.
gowflow
0
 
LVL 31

Expert Comment

by:Rob Henson
Comment Utility
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 29

Expert Comment

by:gowflow
Comment Utility
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
Comment Utility
Gowflow did provide the solution I was looking for but robhenson also provided a solution that works.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now