PDA

View Full Version : [SOLVED:] Delete Empty Table Columns



RayKay
01-04-2019, 02:30 AM
Hi John, your previous code for deleting empty/blank table rows was excellent.
I've tried to adapt it to delete empty/blank table columns. It's not working :(
Any advice please? Thank you.

Working code for deleting empty rows:

Sub DeleteEmptyRows12()
Dim otbl As Table
Dim iRow As Integer
Dim iCol As Integer
Dim b_Text As Boolean

On Error Resume Next
Set otbl = ActiveWindow.Selection.ShapeRange(1).Table
If Not otbl Is Nothing Then
For iRow = otbl.Rows.Count To 1 Step -1
b_Text = False
For iCol = 1 To otbl.Columns.Count
If otbl.Cell(iRow, iCol).Shape.TextFrame.HasText Then b_Text = True
Next iCol
If Not b_Text Then otbl.Rows(iRow).Delete
Next iRow
Else
MsgBox "Please select the table!"
End If
End Sub

Faulty code for deleting empty columns:

Sub DeleteEmptyColumns()
Dim otbl As Table
Dim iCol As Integer
Dim iRow As Integer
Dim b_Text As Boolean


On Error Resume Next
Set otbl = ActiveWindow.Selection.ShapeRange(1).Table
If Not otbl Is Nothing Then
For iCol = otbl.Columns.Count To 1 Step -1
b_Text = False
For iRow = 1 To otbl.Rows.Count
If otbl.Cell(iCol, iRow).Shape.TextFrame.HasText Then b_Text = True
Next iRow
If Not b_Text Then otbl.Columns(iCol).Delete
Next iCol
Else
MsgBox "Please select the table!"
End If
End Sub

Sorry to keep you so busy, my fault for volunteering to improve our old VBA ribbon.
Thank you!

John Wilson
01-08-2019, 03:01 AM
This line is incorrect

If otbl.Cell(iCol, iRow).Shape.TextFrame.HasText Then b_Text = True

Cells are referenced always by (ROW,COLUMN) so it should be


If otbl.Cell(iRow,iCol).Shape.TextFrame.HasText Then b_Text = True

RayKay
01-08-2019, 05:48 AM
THANK YOU !!!