Solved

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

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
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.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

744 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

13 Experts available now in Live!

Get 1:1 Help Now