PDA

View Full Version : How to arrange tables in order as shown in VBA code. Hope everybody help please. Tks



khoav2k
06-23-2021, 01:31 AM
How to arrange the tables in the order shown in the picture using VBA code. Hope everybody help please. thanks

gmayor
06-23-2021, 03:57 AM
The following should work


Sub SortTables()
'Graham Mayor - https://www.gmayor.com - Last updated - 23 Jun 2021
Dim oTable As Table
Dim oRng As Range
Dim i As Integer
Set oRng = ActiveDocument.Range
oRng.Select
Selection.Sort ExcludeHeader:=False, _
FieldNumber:="Paragraphs", _
SortFieldType:=wdSortFieldAlphanumeric, _
SortOrder:=wdSortOrderAscending
Set oTable = ActiveDocument.Tables(1)
Set oRng = oTable.Range
With oRng.Find
.Text = "^p"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
For i = oTable.Rows.Count - 1 To 2 Step -2
oTable.Rows(i).Select
Selection.SplitTable
Next i
Set oTable = Nothing
Set oRng = Nothing
End Sub