Skip to main content

Kutools for Office โ€” One Suite. Five Tools. Get More Done.

How to remove duplicate rows from table in Word document?

Author Sun Last modified

In Word document, there may be some tables with duplicate rows which you want to remove and keep the first appearance one sometimes. In this case, you can choose to remove the duplicate ones one by one manually, also you can choose to use the VBA code.

Remove duplicate rows from table in Word


Remove duplicate rows from table in Word

1. Place the cursor at the table you want to remove the duplicate rows from, press Alt + F11 keys to enable the Microsoft Visual Basic for Applications window.

2. Click Insert > Module to create a new Module.
Insert > Module options in the VBA window

3. Copy below codes and paste them to the new Module script.

VBA: Remove duplicate rows from table in Word

Public Sub DeleteDuplicateRows2()
'UpdatebyExtendoffice20181011
    Dim xTable As Table
    Dim xRow As Range
    Dim xStr As String
    Dim xDic As Object
    Dim I, J, KK, xNum As Long
    If ActiveDocument.Tables.Count = 0 Then
        MsgBox "This document does not have table(s).", vbInformation, "Kutools for Word"
        Exit Sub
    End If
    Application.ScreenUpdating = False
    Set xDic = CreateObject("Scripting.Dictionary")
    If Selection.Information(wdWithInTable) Then
        Set xTable = Selection.Tables(1)
        For I = xTable.Rows.Count To 1 Step -1
            Set xRow = xTable.Rows(I).Range
            xStr = xRow.Text
            xNum = -1
            If xDic.Exists(xStr) Then
'                xTable.Rows(I).Delete
                For J = xTable.Rows.Count To 1 Step -1
                    If (xStr = xTable.Rows(J).Range.Text) And (J <> I) Then
                        xNum = xNum + 1
                        xTable.Rows(J).Delete
                    End If
                Next
                I = I - xNum
            Else
                xDic.Add xStr, I
            End If
        Next
    Else
        For I = 1 To ActiveDocument.Tables.Count
            Set xTable = ActiveDocument.Tables(I)
            xNum = -1
            xDic.RemoveAll
            For J = xTable.Rows.Count To 1 Step -1
                Set xRow = xTable.Rows(J).Range
                xStr = xRow.Text
                xNum = -1
                If xDic.Exists(xStr) Then
    '                xTable.Rows(I).Delete
                    For KK = xTable.Rows.Count To 1 Step -1
                        If (xStr = xTable.Rows(KK).Range.Text) And (KK <> J) Then
                            xNum = xNum + 1
                            xTable.Rows(KK).Delete
                        End If
                    Next
                    J = J - xNum
                Else
                    xDic.Add xStr, J
                End If
            Next
        Next
    End If
    Application.ScreenUpdating = True
End Sub

VBA pasted into the Module window

4. Press F5 key to run the code, then all duplicate rows will be removed.
All duplicate rows are removed from the table

Note: Above code is case sensitive, if you want to remove duplicate rows in case insensitive, you can use below code:

Public Sub DeleteDuplicateRows2()
'UpdatebyExtendoffice20181011
    Dim xTable As Table
    Dim xRow As Range
    Dim xStr As String
    Dim xDic As Object
    Dim I, J, KK, xNum As Long
    If ActiveDocument.Tables.Count = 0 Then
        MsgBox "This document does not have table(s).", vbInformation, "Kutools for Word"
        Exit Sub
    End If
    Application.ScreenUpdating = False
    Set xDic = CreateObject("Scripting.Dictionary")
    If Selection.Information(wdWithInTable) Then
        Set xTable = Selection.Tables(1)
        For I = xTable.Rows.Count To 1 Step -1
            Set xRow = xTable.Rows(I).Range
            xStr = UCase(xRow.Text)
            xNum = -1
            If xDic.Exists(xStr) Then
'                xTable.Rows(I).Delete
                For J = xTable.Rows.Count To 1 Step -1
                    If (xStr = xTable.Rows(J).Range.Text) And (J <> I) Then
                        xNum = xNum + 1
                        xTable.Rows(J).Delete
                    End If
                Next
                I = I - xNum
            Else
                xDic.Add xStr, I
            End If
        Next
    Else
        For I = 1 To ActiveDocument.Tables.Count
            Set xTable = ActiveDocument.Tables(I)
            xNum = -1
            xDic.RemoveAll
            For J = xTable.Rows.Count To 1 Step -1
                Set xRow = xTable.Rows(J).Range
                xStr = UCase(xRow.Text)
                xNum = -1
                If xDic.Exists(xStr) Then
    '                xTable.Rows(I).Delete
                    For KK = xTable.Rows.Count To 1 Step -1
                        If (xStr = xTable.Rows(KK).Range.Text) And (KK <> J) Then
                            xNum = xNum + 1
                            xTable.Rows(KK).Delete
                        End If
                    Next
                    J = J - xNum
                Else
                    xDic.Add xStr, J
                End If
            Next
        Next
    End If
    Application.ScreenUpdating = True
End Sub

If you want to remove duplicate rows in all tables of the document, place the cursor in any place of document outside the table, then apply one of above codes.


Office Tab: Brings tabbed interfaces to Word, Excel, PowerPoint...
Navigate through documents using Office Tab
Enhance your workflow now.      Learn More about Office Tab       Free Download

Do More in Less Time with AI-Enhanced Kutools for Word

Kutools for Word isn't just a set of tools - it's a smart solution designed to boost your productivity. With AI-driven capabilities and the most essential features, Kutools helps you accomplish more in less time:

  • Summarize, rewrite, compose, and translate content instantly.
  • Proofread text in real-time with grammar, punctuation, and style suggestions as you write.
  • Rephrase and translate content while keeping layout, style, and structure untouched.
  • Translate your content into over 40 languages with ease, expanding your reach globally.
  • Receive instant help and intelligent insights based on your current document content.
  • Ask how to complete a task - like removing section breaks - and the AI will guide you or do it for you.
  • Redact sensitive or confidential information in seconds to ensure complete privacy.
  • All tools work seamlessly inside Word, always within reach.
  • Create, refine, translate, summarize and secure documents effortlessly.
  • Improve grammar, clarity, and tone as you write in real time.
  • Rephrase and translate content with no layout or formatting changes.
  • Ask how to complete a task - like removing section breaks - and the AI will guide you or do it for you.
  • All tools work seamlessly inside Word, always within reach.
Learn more about Kutools for Word Download Now
Kutools for Word features

Best Office Productivity Tools

Kutools for Word - Elevate Your Word Experience with Over 100 Remarkable Features!

๐Ÿค– Kutools AI Features: AI Assistant / Real-Time Assistant / Super Polish (Preserve Format) / Super Translate (Preserve Format) / AI Redaction / AI Proofread...

๐Ÿ“˜ Document Mastery: Split Pages / Merge Documents / Export Selection in Various Formats (PDF/TXT/DOC/HTML...) / Batch Convert to PDF...

โœ Contents Editing: Batch Find and Replace across Multiple Files / Resize All Pictures / Transpose Table Rows and Columns / Convert Table to Text...

๐Ÿงน Effortless Clean: Sweap away Extra Spaces / Section Breaks / Text Boxes / Hyperlinks / For more removing tools, head to the Remove group...

โž• Creative Inserts: Insert Thousand Separators / Check Boxes / Radio Buttons / QR Code / Barcode / Multiple Pictures / Discover more in the Insert group...

๐Ÿ” Precision Selections: Pinpoint Specific Pages / Tables / Shapes / Heading Paragraphs / Enhance navigation with more Select features...

โญ Star Enhancements: Navigate to Any Location / Auto-Insert Repetitive Text / Toggle Between Document Windows / 11 Conversion Tools...

๐ŸŒ Supports 40+ Languages: Use Kutools in your preferred language โ€“ supports English, Spanish, German, French, Chinese, and 40+ others!

Kutools and Kutools Plus tabs on the Word Ribbon
๐Ÿ‘‰ Want to try these features? Download Kutools for Word now! ๐Ÿš€
 

โœจ Kutools for Office โ€“ One Installation, Five Powerful Tools!

Includes Office Tab Pro ยท Kutools for Excel ยท Kutools for Outlook ยท Kutools for Word ยท Kutools for PowerPoint

๐Ÿ“ฆ Get all 5 tools in one suite | ๐Ÿ”— Seamless integration with Microsoft Office | โšก Save time and boost productivity instantly

Best Office Productivity Tools

Kutools for Word - 100+ Tools for Word