How do I get a macro to repeat itself?

P

Preacherman

In Microsoft Word table I want to find a letter combination XX, shade that
cell, move to the next XX, shade it, etc. to the end of the table. It works
beautifully in Word Perfect, and there should be a way to do it in Word.
 
S

Shauna Kelly

Hi Preacherman

You can achieve what you want without a macro, if you have Word 2002 or
2003. To do that, select the whole table. Use Edit > Find. In the Find What
box, type your "XX" text. Tick the "Highlight all items found in" box (which
should say "Current selection"). Click Find All, and then Close. You'll see
that Word has selected all cells containing "XX". Now, use Format > Borders
and Shading to shade the cells.

However, if you need a macro to do this, you could use the following code.
You'll need to change XX to the text you need to find.

Sub FindTextInATableAndShadeCell()
' Posted to microsoft.public.word.tables newsgroup
' Shauna Kelly 15/8/2004.

Dim oCell As Cell
Dim sTextToFind As String

If Selection.Information(wdWithInTable) = False Then
MsgBox "Cursor is not currently in a table"
Else

sTextToFind = "XX"

'Find cells in the current table that contain
'sTextToFind, and shade the cell
Selection.Tables(1).Select
With Selection.Find
.ClearFormatting
.Text = sTextToFind
.Wrap = wdFindStop
Do While .Execute
Selection.Cells(1).Shading.BackgroundPatternColorIndex =
wdGray25
Loop
End With
End If
End Sub


If you're not sure what to do with the macro, see

http://www.gmayor.com/installing_macro.htm and

http://www.mvps.org/word/FAQs/MacrosVBA/CreateAMacro.htm


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
G

Greg Maxey

Peacherman,

My code is likely rough, because I am just learning. Something like this
might work

Sub ShadeDesigCells()
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "XX"
.Forward = True
.Wrap = wdFindContinue
Do While Selection.Find.Execute = True
Selection.EndKey
With Selection.Cells.Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray30
End With
Loop
End With
End Sub
 
G

Greg Maxey

PM,

As I suspected, Shauna's appears far better.

Here is a slight adaptation with a input box for identifying the the find
text.
Sub FindTextInATableAndShadeCell()

Dim oCell As Cell
Dim fText As String

If Selection.Information(wdWithInTable) = False Then
MsgBox "Cursor is not currently in a table"
Else

fText = InputBox$("Enter text to find.", "Find")

Selection.Tables(1).Select
With Selection.Find
.ClearFormatting
.Text = fText
.Wrap = wdFindContinue
Do While .Execute
Selection.Cells(1).Shading.BackgroundPatternColor = wdColorGray10
Loop
End With
End If
End Sub
 
P

Preacherman

Shauna Kelly,
Thanks for the idea. I haven't tried it yet, but it sure sounds like it
will work.
Preacherman
 
P

Preacherman

Greg,
I think I will try Shauna's way first, but will come back for yours if I
have trouble with it. By the way, are you related to Mark or Tibbs Maxey?
Preacherman
 
G

Greg Maxey

Shauna's

Suggestion to use the FIND dialog works great and her macro works well also.
I don't know Mark or Tibbs. Unlike a Smith or Jones, there isn't that awful
many of us and I suppose going back far enough there could be relation some
way:)
 
Top