Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-02-03
3
Medium Priority
?
232 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

926 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