Filtering Columns in Word?

J

JuanManuel

Hi,
I’m trying to implement something similar to AutoFilter in Excel. I have
several tables in a document. Two of the columns in each table are called
“Service Description†and “Type of inspection†respectively. Since there are
only three Types of inspection, a simplified version of those two columns
could look as follows:

“Service Description†“Type of Inspectionâ€
Description1 C1
Description2 C1
Description3 HGP
Description4 HGP
Description5 MI
Description6 MI

Let's suppose that every table has something similar to what's shown above.
What I would like to accomplish is that through the click of a button or a
macrobutton field or whichever you consider is the most appropriate event
handler, every table in the document gets filtered based on the Type of
Inspection.

I was thinking about having three macrobutton fields, called "CI", "HGP" and
"MI", so that upon double-clicking "CI" for example, every table in the
document hides/unhides the appropriate rows. (i.e it only shows the rows that
correspond to a CI Inpection Type).

Is there a way to accomplish this? How can I refer to each table in a
document? and moreover, How can I select the appropriate range to hide it
based on the clicked button?

Thank you,

Juan
 
J

Jonathan West

Word doesn't have hidden rows & columns the way Excel does. Therefore, you
can't hide/unhide rows. What you could do is delete unwanted rows.
 
H

Helmut Weber

Hi Jonathan,
Word doesn't have hidden rows & columns the way Excel does. Therefore, you
can't hide/unhide rows. What you could do is delete unwanted rows.

hmm...

Sub Macro2()
With ActiveWindow.View
.ShowHiddenText = False
End With
ActiveDocument.Tables(1).Rows(2).Range.Font.Hidden = True
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

JuanManuel

Hi Helmut,

I have no problem trying to hide rows, especially when I know exactly which
row I'm hiding or when I'm hiding the range going from any row to the end of
the table.

Nevertheless, my problem arises because the current tables can be edited at
any point in time and I'd like the code to keep working.

I’m trying to implement something similar to AutoFilter in Excel. The
document I'm working with has many tables with more than 5 columns. Two of
the
columns in each table are called “Service Description†and “Type of
inspection†respectively. Since there are only three Types of inspection, a
simplified version of those two columns could look as follows:

“Service Description†“Type of Inspectionâ€
Description1 C1
Description2 C1
Description3 HGP
Description4 HGP
Description5 MI
Description6 MI

(The contents are only an example, they vary per table)

Let's suppose that every table has something similar to what's shown above.
Thanks to other mvp's, I ALREADY have macrobutton fields in certain rows of
each table that hide/unhide the remaining rows below it. (i.e., below the row
at which the macrobutton field is positioned).

What I would like to accomplish now is some sort of selective unhiding or
filtering. For example, let's suppose that I have three radiobuttons at the
very first page of the document called "CI", "HGP" and "MI" respectively.
Let's also suppose that at the time of clicking one of the hide/unhide
macrobutton fields, the "CI" radiobutton is selected.

Let's assume that the table has all of its rows hidden before the
macrobutton field is double-clicked. Once it gets, double-clicked and since
the "CI" radiobutton is the one selected, I'd like the table to show the
following:

“Service Description†“Type of Inspectionâ€
Description1 C1
Description2 C1


I was thinking about having three radiobuttons, called "CI", "HGP" and
"MI", so that upon double-clicking "MI" for example, every table in the
document hides/unhides the appropriate rows. (i.e it only shows the rows that
correspond to a MI Inpection Type).

Is there a way to accomplish this?
I'm open to suggestions, can this be accomplished in a different way?

If this is an appropriate method,
How can I select the appropriate range to unhide it based on the selected
radiobutton?

I'm thinking about using the text in the "Type of inspection" column to find
the exact range that needs to be unhidden depending on the radiobutton
selected, but I don't know how. I have:

For i = 1 To TheTable.Rows.Count - StartRow
TextinCell = TheTable.Rows(StartRow + i).Cells(StartColumn +
1).Range.Text
If TextinCell = "C1" Then
TheTable.Rows(StartRow + i).range.font.hidden=false
Else:
TheTable.Rows(StartRow + i).range.font.hidden=false
End If
Next

This procedure locates the column "Type of Inspection" and goes through each
cell testing its text. It's supposed to unhide the appropriate rows depending
on the cell's text. Nevertheless, it doesn't work because of a funny little
square that it includes in each cell to the right of the text. I'm assuming
they are the same squares that appear when you press ctrl + shift + 8. How
can I get read of them??

Or is there a better way to accomplish this?

Thank you,

Juan
 
H

Helmut Weber

Hi Juan,

perhaps like that:

Sub Macro2()
Dim oTbl As Table
Dim lClm As Long ' columnindex
Dim sTmp As String ' text of cell
Dim oClm As Column ' a column
Dim oCll As Cell ' a cell
With ActiveWindow.View
.ShowHiddenText = False
End With
If Selection.Information(wdWithInTable) = False Then
MsgBox "Cursor not in table"
Exit Sub
End If
sTmp = Selection.Cells(1).Range.Text
lClm = Selection.Cells(1).ColumnIndex
For Each oTbl In ActiveDocument.Tables
For Each oCll In oTbl.Columns(lClm).Cells
If oCll.RowIndex > 1 Then
If oCll.Range.Text <> sTmp Then
oCll.Row.Range.Font.Hidden = _
Not oCll.Row.Range.Font.Hidden
End If
End If
Next
Next
End Sub

You got to place the cursor in the cell
with the text that serves as filter.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
I

IpcTrain

When using your WORD filter macro. It stops at the following code:
For Each oCll In oTbl.Columns(lClm).Cells.

I get a run error 5992: Cannot access individual columns because table has
mixed cell width.

The table I'm testing is very small uniformed columns and five rows.
Columns and rows are equal width and height.
 
H

Helmut Weber

Hi IpcTrain,
When using your WORD filter macro. It stops at the following code:
For Each oCll In oTbl.Columns(lClm).Cells.

I get a run error 5992: Cannot access individual columns because table has
mixed cell width.

The table I'm testing is very small uniformed columns and five rows.
Columns and rows are equal width and height.

are you sure?

What does
MsgBox Selection.Tables(1).Uniform
return?
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
H

Helmut Weber

Hi IpcTrain,
It returns True.

then I don't know what is wrong. I'm sorry.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
I

IpcTrain

Mr. Weber,

I figured out what was wrong. Million thank yous. Another related question.

Now that the WORD table is filtered; how do you restore the hidden rows,
unfilter the list?
 
I

IpcTrain

Mr. Weber,

The Hidden Text option must be turned on. I added the following code to the
beginning of your code.

More questions:
1) Is there a maximun number of rows it can filter?
2) Does it matter if the table repeats the header row across sveral pages?
3) Does the table columns, rows and text have to be format a certain way?

'
' UnCkFormatMks Macro
' Checks & unchecks formating marks from Tools, Options, View tab.
'
If ActiveWindow.View.ShowAll = True Then
Application.DisplayStatusBar = True
Application.ShowWindowsInTaskbar = True
Application.ShowStartupDialog = True
With ActiveWindow
.DisplayHorizontalScrollBar = True
.DisplayVerticalScrollBar = True
.DisplayLeftScrollBar = False
.StyleAreaWidth = InchesToPoints(0)
.DisplayVerticalRuler = True
.DisplayRightRuler = False
.DisplayScreenTips = True
With .View
.ShowAnimation = True
.Draft = False
.WrapToWindow = False
.ShowPicturePlaceHolders = False
.ShowFieldCodes = False
.ShowBookmarks = False
.FieldShading = wdFieldShadingAlways
.ShowTabs = True
.ShowSpaces = True
.ShowParagraphs = True
.ShowHyphens = True
.ShowHiddenText = False
.ShowAll = False
.ShowDrawings = True
.ShowObjectAnchors = False
.ShowTextBoundaries = False
.ShowHighlight = True
.DisplayPageBoundaries = True
.DisplaySmartTags = True
End With
End With

Else
Application.DisplayStatusBar = True
Application.ShowWindowsInTaskbar = True
Application.ShowStartupDialog = True
With ActiveWindow
.DisplayHorizontalScrollBar = True
.DisplayVerticalScrollBar = True
.DisplayLeftScrollBar = False
.StyleAreaWidth = InchesToPoints(0)
.DisplayVerticalRuler = True
.DisplayRightRuler = False
.DisplayScreenTips = True
With .View
.ShowAnimation = True
.Draft = False
.WrapToWindow = False
.ShowPicturePlaceHolders = False
.ShowFieldCodes = False
.ShowBookmarks = False
.FieldShading = wdFieldShadingAlways
.ShowTabs = False
.ShowSpaces = False
.ShowParagraphs = True
.ShowHyphens = False
.ShowHiddenText = False
.ShowAll = True
.ShowDrawings = True
.ShowObjectAnchors = False
.ShowTextBoundaries = False
.ShowHighlight = True
.DisplayPageBoundaries = True
.DisplaySmartTags = True
End With
End With
End If
End Sub
 
H

Helmut Weber

Hi,
More questions:
 1) Is there a maximun number of rows it can filter?
There probably is, but it is unknown. ;-)
 2) Does it matter if the table repeats the header row across several pages?
No, but the header will be hidden as well.
If you need more Excel-like features,
things will become very comlicated.
 3)  Does the table columns, rows and text have to be format a certain way?
No
 
I

IpcTrain

Mr Weber,
FYI
The table cells can not be merged. All column must be uniformed. I tested
a table with about 1,050 rows and it worked find, althought the filter toke
10 minutes.

This is an excellent macro for one colunm filter in a WORD table.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top