+ Reply to Thread
Results 1 to 4 of 4

Extract duplicates

Hybrid View

excelexcess Extract duplicates 08-04-2007, 05:18 PM
Leith Ross Hello Excelexcess, This... 08-04-2007, 11:19 PM
FrankBoston Code to move identified rows... 08-04-2007, 11:24 PM
excelexcess These solutions are very... 08-05-2007, 02:25 PM
  1. #1
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Excelexcess,

    This macro will keep only the duplicate products that contain the string "ob" in them. The data must be sorted by product before the macro is run. You can change both the data table cells, and also the location of the new table (code is in blue). The macro is not designed to overwrite the original table. This is installed in the attached workbook.
    Macro Code
    'User: Excelexcess
    'URL: http://www.excelforum.com/showthread.php?t=609757
    'Written: August 4, 2007
    'Author: Leith Ross
    
    Sub CopyDuplicates()
    
      Dim Cnt As Long
      Dim CopyCol As Long
      Dim CopyRng As Range
      Dim I As Long
      Dim LastRow As Long
      Dim MainRng As Range
      Dim Res As Boolean
      Dim Rng As Range
      
    
        Set MainRng = Range("A1:C16")
        Set CopyRng = Range("A21")
        
        ' Copy the list
          CopyCol = CopyRng.Column
          MainRng.Copy Destination:=CopyRng
        
        ' Determine the start and last row
          With Worksheets(CopyRng.Parent.Name)
            FirstRow = CopyRng.Row
            LastRow = .Cells(.Rows.Count, CopyCol).End(xlUp).Row
          End With
          
        ' Remove unique product rows
          For I = LastRow To FirstRow + 1 Step -1
            Cnt = Cnt + 1
              If Cells(I, CopyCol + 2) <> Cells(I - 1, CopyCol + 2) Then
                If Cnt = 1 Then
                   Range(Cells(I, CopyCol), Cells(I, CopyCol + 2)).Delete (xlShiftUp)
                End If
                Cnt = 0
              End If
          Next I
        
        ' Recalculate the row limits
          With Worksheets(CopyRng.Parent.Name)
            FirstRow = CopyRng.Row
            LastRow = .Cells(.Rows.Count, CopyCol).End(xlUp).Row
          End With
          
        ' Keep repeat locations if they contain "ob" in them
          Cnt = 0
            For I = LastRow To FirstRow + 1 Step -1
              Cnt = Cnt + 1
                If Cnt = 1 Then Set Rng = Range(Cells(I, CopyCol), Cells(I, CopyCol + 2))
              Set Rng = Union(Rng, Range(Cells(I, CopyCol), Cells(I, CopyCol + 2)))
              If InStr(1, Cells(I, CopyCol), "ob") > 0 Then Res = True
              If Cells(I, CopyCol + 2) <> Cells(I - 1, CopyCol + 2) Then
                 If Not Res Then Rng.Delete (xlShiftUp)
                 Cnt = 0
                 Res = False
              End If
          Next I
                  
    End Sub
    Sincerely,
    Leith Ross
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1