# Word VBA: How do I make a macro run until the end of the file



## HowdeeDoodee (Aug 26, 2004)

I have this macro. Now, to run the macro I have to hold down the Alt+R key continuously until the macro repeats until the macro reaches the end of the file. How do I change the macro so when i hit the shortcut key, the macro repeats on its own without me having to hold down the shortcut key?


```
Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "[|]"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.Delete Unit:=wdCharacter, Count:=1
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "^t"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.Cut
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "change^t"
        .Replacement.Text = ""
        .Forward = False
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.Find.ClearFormatting
       With Selection.Find
        .Text = "^p"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.TypeText Text:="[OR]"
    Selection.Paste
    Selection.MoveRight Unit:=wdCharacter, Count:=1
```
Thank you in advance for replies.


----------



## Jimmy the Hand (Jul 28, 2006)

Well, there is the primitive way, and there is the sophisticated way. 
I tell you how it's done in the primitive way. 
Put a "Do" command to the beginning of the macro, and put a "Loop" command to the end of the macro.
When you hit the shortcut, the macro will run until no match is found, then stop with an error message. 
This is good for a few-time use, for yourself. If you create an application for other users, however, the sophisticated way is recommended. It's the same Do...Loop cycle, plus a conditional exit from the loop. Something like:

If nomatch Then Exit Do

I most probably could do the coding, if you provide a sample text to test the modified macro on.


----------



## HowdeeDoodee (Aug 26, 2004)

Thank you Jimmy.

I also found an answer much like your own. If a user wants to run a macro 100 times, here is how to do it in code.

To loop the macro in code add this around your macro (after the sub line & any declarations)

Sub NameOfTheMacro

For i = 1 to 100

'your code

next i

end sub

example:
Sub Beeps()
For i = 1 To 50
Beep 'your macro
Next i
End Sub

Thank you again, Jimmy.


----------

