Solved

Cut and Paste Rows to another sheet based on duplicate value

Posted on 2013-01-04
11
277 Views
Last Modified: 2013-01-08
Good Afternoon

I am looking for a macro which will compare the values in column B and if duplicates exist, cut each instance of that duplicate row and paste it in a new sheet. For example:

A                   B
Test               1
Test               1
Test               2
Test               3
Test               3

In this example the rows with 1 and 3 would be cut and pasted into a new sheet.

Thanks
0
Comment
Question by:Washcare
  • 6
  • 4
11 Comments
 
LVL 10

Expert Comment

by:etech0
ID: 38744281
You can try something like this:

Sub Duplicates()
Dim x As Integer
    
Columns("A:B").Select
Range("B1").Activate
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("B1"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
    .SetRange Range("A:B")
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
    
x = 2
Do While Cells(x, 1).Value <> ""
    If Cells(x, 2).Value = Cells(x - 1, 2).Value Then
        Rows(x).Select
        Selection.Cut
        Sheets("NameOfSecondSheet").Select
        Range("A1").Select
        Do Until ActiveCell.Value = ""
            ActiveCell.Offset(1, 0).Select
        Loop
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Sheets("NameOfFirstSheet").Select
    End If
    x = x + 1
Loop
End Sub

Open in new window

0
 
LVL 29

Expert Comment

by:gowflow
ID: 38748847
Hi
Is this what your looking for ?
Load the file and make sure your macroes are enabled and run the button Transfer Duplicates.

Let me know
gowflow
TransferDup.xls
0
 

Author Comment

by:Washcare
ID: 38750434
gowflow

Sorry for the delay in my reply, its been a busy weekend :(

Thank you for your reply's, I am sorry but I do not think I was clear enough, I require that all the rows with duplicates are copied, including the original row. i.e.

Test 1
Test 1

Both rows would be copied to the new sheet, not just one row.

Thank you
0
 
LVL 29

Expert Comment

by:gowflow
ID: 38750555
ok fine. Will this do it ?
gowflow
TransferDup.xls
0
 

Author Comment

by:Washcare
ID: 38750655
gowflow

That's a lot closer, you seem to be checking both column A and B have matching duplicates?

Could the cut and paste just work on duplicates in column B, so from you example spreadsheet (which are great, thank you) I would expect the row:

do                   6

To also be cut and pasted as its the 6 which is the duplicate.

Thanks
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 29

Expert Comment

by:gowflow
ID: 38750719
So only duplicate in Col B ??? Not Col A as well ?
gowflow
0
 
LVL 29

Expert Comment

by:gowflow
ID: 38750741
Boy I was mislead and did not understand your post !!!
Chk this and let me know.
gowflow
TransferDup.xls
0
 

Author Comment

by:Washcare
ID: 38750800
gowflow

Yes only duplicates in column B. Thanks for the example, this still leaves the following row:

go              6

on Sheet1?

Thanks
0
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 38751923
wow that was not an easy one.
try this I hope we got it.
gowflow
TransferDupFinal.xls
0
 

Author Comment

by:Washcare
ID: 38754140
gowflow

Many thanks, works great.

Regards
0
 
LVL 29

Expert Comment

by:gowflow
ID: 38754668
Finnalllllyyy !! shame on me so simple request that took me several attempt to adapt correctly. Glad I could at least help you. !!!
gowflow
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
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.

747 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

10 Experts available now in Live!

Get 1:1 Help Now