Consulting

Results 1 to 3 of 3

Thread: Delete Empty Table Columns

  1. #1
    VBAX Contributor
    Joined
    Dec 2018
    Location
    South London
    Posts
    115
    Location

    Delete Empty Table Columns

    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!

  2. #2
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    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

    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  3. #3
    VBAX Contributor
    Joined
    Dec 2018
    Location
    South London
    Posts
    115
    Location

    Deleting columns in a table

    THANK YOU !!!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •