Consulting

Results 1 to 4 of 4

Thread: VBA - Remove Duplicate rows from an entire worksheet

  1. #1
    VBAX Newbie
    Joined
    Oct 2023
    Posts
    2
    Location

    VBA - Remove Duplicate rows from an entire worksheet

    Hi everyone!

    I need help with VBA removing duplicate rows.

    My colleagues are exporting reports from Power BI. Then, they are combined on a single worksheet. Each report can contain various numbers of rows - hundreds or dozens of thousands. After each report's dataset, an empty row is added, and then a message row with a list of filters applied in Power BI. You can see an example of the output file as attached. This is what combined data looks like.

    Eventually, I don't need those "filter messages," but removing them is relatively easy. However, removing duplicate rows presents an issue.

    I have recorded a macro in Excel. When recording the macro, I selected the entire worksheet, and it worked well in the workbook where it was recorded. But when running the same macro on a different dataset, it gives an error, specifically when the number of rows is larger.

    Sub RemoveDuplicates()
    Cells.Select
    ActiveSheet.Range("$A$1:$V$5907").RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6 _
    , 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22), Header:=xlYes
    End Sub
    Can you help me with the code that would remove duplicate rows from an entire worksheet without specifying the number of rows?

    Thank you!!!

    P.S. I am using Excel 365 for Windows, build 2307
    Attached Files Attached Files
    Last edited by Aussiebear; 10-09-2023 at 03:17 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Probably something like this

    Option Explicit
    
    
    Sub RemoveDuplicates_1()
        Dim r1 As Range, r2 As Range, r As Range
    
    
        With ActiveSheet
            Set r1 = .Cells(2, 1)
            Set r2 = .Cells(.Rows.Count, 1).End(xlUp)
            Set r = Range(r1, r2)
        
            On Error Resume Next
            r.SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete
            r.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
            On Error GoTo 0
        
            .Cells(1, 1).CurrentRegion.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22), Header:=xlYes
        End With
        
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Welcome to VBAX Andreys.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Newbie
    Joined
    Oct 2023
    Posts
    2
    Location
    I have found a solution:
    Sub RemoveDuplicates()
        Dim ws As Worksheet
        Dim rng As Range
        Dim n As Long
        Dim i As Long
        Set ws = ActiveSheet
        Set rng = ws.UsedRange
        n = rng.Columns.Count
        ReDim varArray(0 To n - 1)
        For i = 0 To n - 1
            varArray(i) = i + 1
        Next i
        ws.UsedRange.RemoveDuplicates Columns:=(varArray), Header:=xlYes
    End Sub
    Last edited by Aussiebear; 10-09-2023 at 03:15 PM. Reason: Added code tags to supplied code

Posting Permissions

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