user3060546
user3060546

Reputation: 1

use array to search for words in a Word document

I have a Word document containing a result report of a weekly athletics meeting. There a several results grouped together for each of a range of events.

Here is a brief extract...

Men 18-19 Discus Throw 1kg F36:

  1. Shaun Markham, HCTU, 10.26m.

Women 15-17 Discus Throw 1kg:

  1. Elizabeth Jones, TAIR, 36.34m; 2. Courtney Martin, ODR, 32.61m; 3.

    Jessie Scurr, ODR, 26.24m.

Women 15-17 Hammer Throw 3.00kg:

  1. Elizabeth Jones, TAIR, 49.04m; 2. Courtney Martin, ODR, 42.34m; 3.

    Melina Palmer, TAIR, 29.24m.

Men 14&U High Jump :

  1. Benjamin Smith, ODR, 1.65m.

Men 18-19 High Jump :

  1. Brent Cheshire, ODR, 1.80m.

The vba macro I have written is

Selection.HomeKey Unit:=wdStory
  With Selection.Find
 .ClearFormatting
 .Text = "discus"
 .Forward = True
 .Wrap = wdFindContinue
 .Format = False
 .MatchCase = False
 .MatchWholeWord = False
 .MatchAllWordForms = False
 .MatchSoundsLike = False
 .MatchWildcards = False
 .Execute
 Selection.Extend
 Selection.Copy
 Selection.MoveUp Unit:=wdLine, Count:=1
 Selection.EndKey Unit:=wdLine
 Selection.TypeParagraph
 Selection.PasteAndFormat (wdFormatOriginalFormatting)
 Selection.Extend
 Selection.Extend
 Selection.Extend
 Selection.Range.Bold = True
 Selection.Font.Grow
 End With
 End Sub

...finds the first occurence of a result for discus and creates a bold heading on a new line immediately above that first result.

What I want to do is repeat this process for the block of results for each event...ie create a heading for Hammer, High Jump etc, etc.

I could just repeat this code for every event (there could be up to 30 or 40 possible events but in any given week there will only be perhaps 15...so not every event will feature in a results report) but there must be a loop or such like that could cycle through each event listed in (perhaps) an array.

This would keep the macro compact and would mean that if an event name changes the only modification required to the macro would be in the array.

Can someone help me?

Upvotes: 0

Views: 858

Answers (1)

Are you looking for something like this?

Sub MakeAllHeadlines()
    Dim i As Long
    Dim eventNames As Variant
    eventNames = Array("discus", "Hammer", "High Jump", "etc")
    For i = LBound(eventNames) To UBound(eventNames)
        MakeEventHeadline eventNames(i)
    Next i
End Sub

Sub MakeEventHeadline(ByVal eventName As String)
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Text = eventName
        '...rest of your code as written in your question
    End With
End Sub

Upvotes: 2

Related Questions