How to remove duplicate rows from table in Word document?
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.
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
4. Press F5 key to run the code, then all duplicate rows will be removed.
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.
Tabbed browsing & editing multiple Word documents/Excel workbooks as Firefox, Chrome, Internet Explore 10! |
You may be familiar to view multiple webpages in Firefox/Chrome/IE, and switch between them by clicking corresponding tabs easily. Here, Office Tab supports similar processing, which allow you to browse multiple Word documents or Excel workbooks in one Word window or Excel window, and easily switch between them by clicking their tabs. |
![]() |
Best Office Productivity Tools
Kutools for Word - Elevate Your Word Experience with Over 100 Remarkable Features!
Dive into the highlighted features below or click here to explore the full power of Kutools for Word.
๐ Document Mastery: Split Pages / Merge Documents / Export Selection in Various Formats (PDF/TXT/DOC/XLSX) / Batch Convert to PDF / Export Pages as Images / Print Multiple Files at once ...
โ 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 / All Headers / Text Boxes / Hyperlinks / For more removing tools, head to our Remove Group
โ Creative Inserts: Insert Thousand Separators / Check Boxes / Radio Buttons / QR Code / Barcode / Diagonal Line Table / Equation Caption / Image Caption / Table Caption / Multiple Pictures / Discover more in our Insert Group
๐ Precision Selections: Pinpoint specific pages / tables / shapes / heading paragraphs / Navigate with ease using our Select Group
โญ Star Enhancements: Navigate swiftly to any location / auto-insert repetitive text / seamlessly toggle between document windows / 11 Conversion Tools ...
Transform your Word tasks with Kutools. ๐ Download with 30-day trial Now ๐.
