1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by HowdeeDoodee, Sep 5, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. HowdeeDoodee

    HowdeeDoodee Thread Starter

    Joined:
    Aug 26, 2004
    Messages:
    608
    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?

    Code:
        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.
     
  2. Jimmy the Hand

    Jimmy the Hand

    Joined:
    Jul 28, 2006
    Messages:
    1,220
    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.
     
  3. HowdeeDoodee

    HowdeeDoodee Thread Starter

    Joined:
    Aug 26, 2004
    Messages:
    608
    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.
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/498511