Solved

Excel 2010 Copy Duplicates to a new worksheet

Posted on 2014-03-17
12
2,831 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
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 29

Expert Comment

by:gowflow
ID: 39934019
Can we sort the worksheet first ?
gowflow
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

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

Accepted Solution

by:
gowflow earned 450 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 32

Assisted Solution

by:Rob Henson
Rob Henson earned 50 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 29

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 32

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 29

Expert Comment

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

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 29

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Conditional Median Problem 1 15
Alphabetical Order for Letters 2 21
Excel VBA 4 27
VBA: Insert new column and remove first character of string 9 23
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

773 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