Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

777 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