Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel 2010 Copy Duplicates to a new worksheet

Posted on 2014-03-17
12
Medium Priority
?
3,570 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 34

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 34

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 34

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

885 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