Solved

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

Posted on 2013-02-03
3
214 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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