Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating a Loop in VBA to clear an Excel Rec of duplicate data

Posted on 2013-11-04
9
Medium Priority
?
202 Views
Last Modified: 2013-11-20
Hi Guys, I need to create a Macro that can Loop down Column E find a Duplicate ID and cut and paste that line onto a different tab. The Duplicate ID must have a 0 or blank space in Column F so the correct ID is left behind. Below you see the column "FO MTM" has a blank space and  2 exact numbers in the "GBO ID" column, the 2nd row need to be cut and paste to a tab called "Exceptions". Can anyone help? I enclose an attachment of the actual Rec


Book      Currency      Trade ID                     GBO ID                   FO MTM                        GL MTM
GFALCOFRAM      GBP      6530805      3811642.28      315,443      315,443
GFALCOFRAM      GBP      6416721      3811642.28            315,443
DummyRec.xlsx
0
Comment
Question by:Justincut
[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
  • 5
  • 4
9 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39622246
This macro assumes that the duplicate is immediately below the one to be kept.

Sub movedups()
    Dim cel As Range
    Dim i As Long
    For i = Range("E" & Rows.Count).End(xlUp).Row To 4 Step -1
        Set cel = Range("E" & i)
        cel.Select
        If cel.Value <> "" And cel.Value = cel.Offset(-1).Value And (cel.Offset(, 1) = 0 Or cel.Offset(, 1) = "") Then
            cel.EntireRow.Copy Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1)
            cel.EntireRow.Delete
        End If
    Next i
End Sub
0
 

Author Comment

by:Justincut
ID: 39622372
Hi, the file is 50,000 rows of data and is not necessarily below but could be 10,000 rows away or could be above the genuine trade ID. Could you adapt your code?
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39622720
Try

Sub movedups()
    Dim cel As Range
    Dim i As Long
    For i = Range("E" & Rows.Count).End(xlUp).Row To 4 Step -1
        Set cel = Range("E" & i)
        cel.Select
        If cel.Value <> "" And WorksheetFunction.CountIf(cel.EntireColumn, cel.Value) > 1 And (cel.Offset(, 1) = 0 Or cel.Offset(, 1) = "") Then
        Stop
            cel.EntireRow.Copy Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1)
            cel.EntireRow.Delete
        End If
    Next i
End Sub
0
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.

 

Author Comment

by:Justincut
ID: 39622895
What does "Sheet2Range" refer to? I would like to cut the duplicate Ids and paste it to a tab called "Exceptions"
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39623465
Change

            cel.EntireRow.Copy Sheet2.Range("A" & Rows.Count).End(xlUp).Offset(1)

to

            cel.EntireRow.Copy Sheets("Exceptions").Range("A" & Rows.Count).End(xlUp).Offset(1)
0
 

Author Comment

by:Justincut
ID: 39624023
Actually, how can I adapt the code so it Pastes the Duplicates into the Range ("P4") in the
same tab?
0
 

Author Comment

by:Justincut
ID: 39627122
Either this or insert a tab in the TEXT file and call the Tab "Exceptions". How do I do this?
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 2000 total points
ID: 39627402
My previous comment tells how to do this to a tab called Exceptions.
0
 

Author Comment

by:Justincut
ID: 39636665
Hi, its a text file so there is no tab so I have to insert a tab and call it "Exceptions" and then paste it.How do I adapt the code?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

596 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