PDA

View Full Version : Help with conditionally formatting columns



gadgetgary
02-27-2011, 08:39 PM
Hello,

In word 2007 trying to conditionally format columns of many tables in a long document I have inherited.

For each column that has the following text in the header row.
"Req'd"
I would like to horizontally centre the contents of each cell in the column.

I can loop through the tables with a for next statement but need help with conditionally formatting the column.

Cheers

Gary

mdmackillop
03-02-2011, 04:18 PM
Can't figure out how to get rid of the Selection, but give this a try
Sub TableGen()
Dim i As Long
Dim MyTable As Table
Dim Cel As Cell
With ActiveDocument
For i = 1 To .Tables.Count
Set MyTable = .Tables(i)
For Each Cel In MyTable.Rows(1).Cells
If InStr(1, Cel.Range.Text, "Req'd") > 0 Then
MyTable.Columns(Cel.ColumnIndex).Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End If
Next

Next
End With
End Sub

gadgetgary
03-02-2011, 08:34 PM
Thanks for the reply.

It seemed to have trouble with the "Req'd" text.
I tried it with another heading and it worked beautifully.
So a quick Replace of Req'd with RxRx, run the modified macro and Replace back the other way :)

That work for 98% of the tables, there where just a few pesky ones with merged cells which the code did not like, but as there where only a few, I could easily do those manually.

Many thanks

Gary :hifive: