Shading cells based on content

A

AOH

Hi, I'm looking for a way to automatically add shading to a row of
cells. What I have is a two-column table, and I'd like to shade the
rows which contain a particular word in the first cell. I can't find a
way to do this using table styles, so I think a macro is the way
forward. My VB is pretty much nonexistent but I'm willing to
learn...can anyone point me in the right direction to get me started?
Using Word 2003, Win XP. Thanks
 
G

Greg Maxey

AOH,

I am a VBA beginner myself and so the code I am providing can probably be
improved. Still it seems to work.
Sub ScratchMacro()
'Looks for the text string "????" in the first column and shades
'the corresponding row. Replace "????" in the InStr expression below with
the
'word or text string you want to search for
Dim oCell As Cell
Dim oRow As Row
Dim oCol As Column
Dim oTable As Table
Dim i As Long
If Selection.Information(wdWithInTable) = True Then
Set oTable = Selection.Tables(1)
Set oCol = Selection.Columns(1)
i = 0
Else
MsgBox "Selection is not in a table"
Exit Sub
End If
For Each oCell In oCol.Cells
i = i + 1
If InStr(oCell.Range.Text, "????") >= 1 Then
Set oRow = oTable.Rows(i)
oRow.Shading.BackgroundPatternColorIndex = wdGray25
Set oRow = Nothing
End If
Next
End Sub
 

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