+ Reply to Thread
Results 1 to 5 of 5

Create Unique List in Shortest Time

Hybrid View

  1. #1
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Create Unique List in Shortest Time

    I have a list of project numbers that is over 7000 rows long [example attached].
    Many numbers are repeated. This list comes out of a financial reporting system.
    I need to extract a list of unique numbers in numerical order.
    I have two solutions working, one with SUMPRODUCT formulas and one with MACRO.
    Here's the MACRO solution.
    But it takes about 20 seconds to execute.
    I am just looking to cut down on execution time. I am using this macro a few times on different lists in the same program.

    Is there a better [faster] way to do this?

    Sub UniqueOrder()
    Sheets("Sheet1").Select
    Dim LR As Long
    LR = Range("A" & Rows.Count).End(xlUp).Row
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    For i = LR To 2 Step -1
        If WorksheetFunction.CountIf(Columns("A:A"), Cells(i, "A")) > 1 Then _
                             Rows(i).EntireRow.Delete Shift:=xlUp
    Next i
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    End Sub

    Thanks for any helpful hints.
    modytrane
    Attached Files Attached Files
    Last edited by modytrane; 06-17-2009 at 03:55 PM. Reason: solved

  2. #2
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Create Unique List in Shortest Time

    Using the Advanced filter with the Unique records option checked is probably the fastest way to do it.
    Everyone who confuses correlation and causation ends up dead.

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Create Unique List in Shortest Time

    echoing romperstomper... re: Advanced Filter

    on an aside bear in mind that deletion of rows is a Volatile action so normally a good idea to set Calc to Manual beforehand and reset to original setting thereafter:

    Sub UniqueOrder()
    Dim LR As Long, i As Long
    Dim xlCalc as xlCalculation
    LR = Cells(Rows.Count,"A").End(xlUp).Row
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
        For i = LR To 2 Step -1
            If .CountIf(Columns("A:A"), Cells(i, "A")) > 1 Then Rows(i).EntireRow.Delete
        Next i
        .calculation = xlCalc
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    End Sub

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Create Unique List in Shortest Time

    Should see an increase with this

    Sub UniqueOrder2()
    
        Sheets("Sheet1").Select
        Dim LR As Long
        Dim lngRow As Long
        Dim strLastID As String
        Dim lngLastRow As Long
        
        
        Application.ScreenUpdating = False
        Application.EnableEvents = False
        
        LR = Range("A" & Rows.Count).End(xlUp).Row
        strLastID = Cells(LR, 1).Value
        lngLastRow = LR - 1
        
        For lngRow = LR To 1 Step -1
            If Cells(lngRow, 1).Value <> strLastID Then
                If lngLastRow - lngRow > 0 Then
                    Range(Cells(lngRow + 1, 1), Cells(lngLastRow, 1)).EntireRow.Delete shift:=xlUp
                End If
                strLastID = Cells(lngRow, 1)
                lngLastRow = lngRow - 1
            End If
        Next
        Application.ScreenUpdating = True
        Application.EnableEvents = True
    
    End Sub
    Cheers
    Andy
    www.andypope.info

  5. #5
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    Re: Create Unique List in Shortest Time

    Thanks Andy,
    That routine works great.
    It speeds up the process significantly.
    Thank You,
    modytrane.

+ 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