1

I found a great solution from this post: Removing duplicate rows after checking all columns

Sub Remove_DuplicateRows()
    Dim intArray As Variant, i As Integer
    Dim rng As Range
    Dim ws As Worksheet
    
    Call Open_Workbook
        
    Set ws = Workbooks("Sales2021.xlsm").Sheets("Reporting Template")
    ws.Activate
    Set rng = ws.UsedRange.Rows
    With rng
        ReDim intArray(0 To .Columns.Count - 1)
        For i = 0 To UBound(intArray)
            intArray(i) = i + 1
        Next i
        .RemoveDuplicates Columns:=(intArray), Header:=xlYes
    End With
End Sub

I tried the script, and wanted to adjust to my case: I want to delete all duplicated rows based on all columns except the first column (i.e., columns B to U). Should I use ws.Range("B2:U3000") instead of UsedRange?

3
  • What did YOU try. Please post the code you tried to solve this problem. Commented Oct 11, 2022 at 9:50
  • The above code is the one I tried. I used the solution from the previous post and adjusted a bit to my data, but get stuck how to exclude the first column Commented Oct 11, 2022 at 10:07
  • 1
    The example in the link has a double loop, rows and columns. You'll need both loops. UsedRange.Rows iterates through all rows, I think you don't want to change that. The line that needs the change is: "For Col = Rng.Columns.Count To 1 Step -1". You want to change the starting point, so "To 2 step -1" instead of "To 1 step -1" Commented Oct 11, 2022 at 10:14

1 Answer 1

1

You can either use ws.Range("B2:U3000") or below code

Set rng = ws.UsedRange.Offset(0, 1).Resize(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count - 1)

The final code should look like this.

Sub Remove_DuplicateRows()
    Dim intArray As Variant, i As Integer
    Dim rng As Range
    Dim ws As Worksheet
    
    Call Open_Workbook
        
    Set ws = Workbooks("Sales2021.xlsm").Sheets("Reporting Template")
    ws.Activate
    Set rng = ws.UsedRange.Offset(0, 1).Resize(ws.UsedRange.Rows.Count, ws.UsedRange.Columns.Count - 1)
    With rng
        ReDim intArray(0 To .Columns.Count - 1)
        For i = 0 To UBound(intArray)
            intArray(i) = i + 1
        Next i
        .RemoveDuplicates Columns:=(intArray), Header:=xlYes
    End With
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.