Solved

Looking for VB Macro to sort specific cells for duplication and only remove part based on criteria

Posted on 2013-02-03
3
211 Views
Last Modified: 2013-02-05
Not sure if possible. Thanks to previous help by the Experts was able to have a Macro that sorted duplicates, but report changed and I need to have this question tweaked.  Report now pulls in the Client# and each part for the record. So if each record has 3 part#'s on it then it could have 3 results and then when worked on again the client number's 14th digit changes and will have a repeat of the parts with the new results if any changes occurred.

I need to be able to count based on the most current record, and looking to remove any previous record but without removing any entries on the current record. If that makes sense.  

I'm attaching an example of the report. Everything would be based on Column E and can run all macro's before and after for the rest of the information, just this part I need to have for the calculations.

Client Record#      Purchase code (example with generic part#)      Date of Purchase
13013178910100      i3207      01/01/2013
13013178910100      i3206      01/01/2013
13013178910100      i3205      01/01/2013
13013178910101      i3207      01/08/2013
13013178910101      i3206      01/08/2013
13013178910101      i3205      01/08/2013
13013178910102      i3207      01/15/2013
13013178910102      i3206      01/15/2013
13013178910102      i3205      01/15/2013

so would need to keep all lines ending with 02 and delete the other rows of the same record.
Report-Example.xlsm
0
Comment
Question by:NYANBCNY32
  • 2
3 Comments
 
LVL 14

Expert Comment

by:Faustulus
ID: 38849124
Great! I'll have a look at it later today.
0
 
LVL 14

Accepted Solution

by:
Faustulus earned 500 total points
ID: 38849338
Please try this code.
Observe that I changed the ID column to colum B. If you test this code on your workbook as posted please change NwsID = 5 since you have the IDs in column E in that workbook.
Option Explicit

    Enum Nws                    ' Worksheet navigation
        NwsFirstDataRow = 2
        NwsID = 2               ' columns: 2 = B / change as required
    End Enum

Sub DeletePrevious()

    Dim Ws As Worksheet
    Dim GoodIDs As String
    Dim ID As String
    Dim R As Long
    
    Set Ws = ActiveSheet
    Application.ScreenUpdating = False
    
    For R = LastRow(NwsID, Ws) To NwsFirstDataRow Step -1
        ID = Ws.Cells(R, NwsID).Value
        If InStr(GoodIDs, ID) = 0 Then
            If InStr(GoodIDs, Left(ID, Len(ID) - 1)) Then
                Ws.Rows(R).EntireRow.Delete
            Else
                GoodIDs = "|" & ID & GoodIDs
            End If
        End If
    Next R
    
    Application.ScreenUpdating = True
End Sub

Private Function LastRow(Optional ByVal Col As Variant, _
                         Optional Ws As Worksheet) As Long
    ' 0059 V 3.2 Apr 2, 2012

    ' Return the number of the last non-blank row in column Col.
    ' Specify the column as string or number
    ' If no column is specified,
      ' return the last row from column A.
    ' If no worksheet is specified
      ' return the result from the currently active sheet.
    
    Dim R As Long
    
    If Ws Is Nothing Then Set Ws = ActiveSheet
    If VarType(Col) = vbError Then Col = 1
    With Ws
        R = .Cells(.Rows.Count, Col).End(xlUp).Row
        With .Cells(R, Col)
            ' in a blank column the last used row is 0 (= none)
            If R = 1 And .Value = vbNullString Then R = 0
            ' include all rows of a merged range
            LastRow = R + .MergeArea.Rows.Count - 1
        End With
    End With
End Function

Open in new window

0
 

Author Closing Comment

by:NYANBCNY32
ID: 38857871
Again thank you for the help, it works perfectly!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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;…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
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 …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

910 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

20 Experts available now in Live!

Get 1:1 Help Now