hide table columns

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

hide table columns

Post by Robie »

Hi

I know Microsoft don't provide a way to hide columns as we can with rows in a table. :groan: :sad:

I need to hide columns where *all* cell in a column are hidden or blank. Is there a way to do this please?

So, basically if 1st column is 'hidden' then the top, left and bottom border are should be hidden.
If middle column then either top, bottom and left/right column to be hidden.
If last column then top, bottom and right border to be hidden.

Let me know if this is possible or not (otherwise I get back to the management saying the users will have to live with empty columns). I have lots of document where this is required.

Thanks.
Robie
Last edited by Robie on 16 Jul 2015, 14:28, edited 1 time in total.

User avatar
HansV
Administrator
Posts: 78596
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: hide table columns

Post by HansV »

The requirements aren't clear to me - making borders transparent (I assume that's what you mean by hiding borders) is not the same as hiding a column or row.

You could paste an Excel table as an Excel Worksheet object. It's easy to hide rows or columns in Excel
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: hide table columns

Post by Robie »

HansV wrote:The requirements aren't clear to me - making borders transparent (I assume that's what you mean by hiding borders) is not the same as hiding a column or row.

You could paste an Excel table as an Excel Worksheet object. It's easy to hide rows or columns in Excel
Thanks Hans.

Yes, wrong phrase. Doh.
I mean make borders transparent. Excel is not an option I am afraid. The documents already contain tables with hidden cells in columns.

User avatar
HansV
Administrator
Posts: 78596
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: hide table columns

Post by HansV »

Making the borders transparent won't remove the space taken up by the empty column. But what's stopping you from making the borders transparent?
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: hide table columns

Post by Robie »

HansV wrote:Making the borders transparent won't remove the space taken up by the empty column. But what's stopping you from making the borders transparent?
I guess nothing except that there are lots of tables and lots of documents. :sad:
I can of course do them by hand - it will just take time.

Thanks for looking into Hans - no worries. I will do it by hand.

User avatar
HansV
Administrator
Posts: 78596
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: hide table columns

Post by HansV »

This is a beginning for a macro. It's not perfect.

Code: Select all

Sub HideEmptyCol()
    Dim tbl As Table
    Dim col As Column
    Dim cel As Cell
    Dim f As Boolean
    For Each tbl In ActiveDocument.Tables
        For Each col In tbl.Columns
            f = True
            For Each cel In col.Cells
                If Len(cel.Range.Text) > 2 Then
                    f = False
                    Exit For
                End If
            Next cel
            If f Then
                For Each cel In col.Cells
                    cel.Borders(wdBorderTop).Visible = False
                    cel.Borders(wdBorderBottom).Visible = False
                    Select Case col.Index
                        Case 1
                            cel.Borders(wdBorderLeft).Visible = False
                        Case tbl.Columns.Count
                            cel.Borders(wdBorderRight).Visible = False
                    End Select
                Next cel
            End If
        Next col
    Next tbl
End Sub
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: hide table columns

Post by Robie »

HansV wrote:This is a beginning for a macro. It's not perfect.

Code: Select all

Sub HideEmptyCol()
    Dim tbl As Table
    Dim col As Column
    Dim cel As Cell
    Dim f As Boolean
    For Each tbl In ActiveDocument.Tables
        For Each col In tbl.Columns
            f = True
            For Each cel In col.Cells
                If Len(cel.Range.Text) > 2 Then
                    f = False
                    Exit For
                End If
            Next cel
            If f Then
                For Each cel In col.Cells
                    cel.Borders(wdBorderTop).Visible = False
                    cel.Borders(wdBorderBottom).Visible = False
                    Select Case col.Index
                        Case 1
                            cel.Borders(wdBorderLeft).Visible = False
                        Case tbl.Columns.Count
                            cel.Borders(wdBorderRight).Visible = False
                    End Select
                Next cel
            End If
        Next col
    Next tbl
End Sub
Thank you so much Hans.

It was just the start I needed. My mind just went blank when thinking about this issue.

Thank you once again.