Share via

Word Re-set after the 3rd

SteveD 430 Reputation points
2026-04-27T00:35:40.78+00:00

Hello from Steve

Can the below be re-set after the third selection

I do more than one in the same document.

When I go to use it the second time in the same document it puts in the third selection' I need it to restart by putting in the first selection please.

Option Explicit

Dim gRunCount As Long

Sub OneButtonMacro()

'Increment run counter each time the macro is pressed

gRunCount = gRunCount + 1

Select Case gRunCount

    Case 1

        ProcessSelection 1

    Case 2

        ProcessSelection 2

    Case 3

        ProcessSelection 3

    Case Else

        'After third time, stay at 3 or reset as desired

        gRunCount = 3

        ProcessSelection 3

End Select
```End Sub

Private Sub ProcessSelection(ByVal n As Long)

Selection.Find.ClearFormatting

With Selection.Find

.Text = "|"

.Replacement.Text = " "

.Forward = True

.Wrap = wdFindContinue

.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:=CStr(n)

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

Selection.Font.Color = wdColorRed

Selection.Font.Bold = True

Selection.range.HighlightColorIndex = wdYellow

Selection.MoveLeft Unit:=wdCharacter, Count:=1

Selection.ParagraphFormat.TabStops.Add _

Position:=CentimetersToPoints(18.75), _

Alignment:=wdAlignTabLeft, _

Leader:=wdTabLeaderSpaces

Selection.EndKey Unit:=wdLine

Selection.TypeText Text:="$"

Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

Selection.Font.Bold = True

Selection.Font.Color = wdColorBlue

Selection.HomeKey Unit:=wdLine

Selection.MoveRight Unit:=wdCharacter, Count:=1


Developer technologies | Visual Basic for Applications
0 comments No comments

2 answers

Sort by: Most helpful
  1. Kai-H 16,355 Reputation points Microsoft External Staff Moderator
    2026-04-27T08:03:51.5866667+00:00

    Hi, SteveD

    Your gRunCount keeps its value because it is a module-level variable, and those normally stay alive until Word stops running the code or the project gets reset.

    So the easiest fix is just to cycle it back to 1 after 3:

    Sub OneButtonMacro()End Sub
        gRunCount = (gRunCount Mod 3) + 1
        ProcessSelection gRunCount
    

    That will make it go 1, 2, 3, then back to 1 every time you press the button.

    If you prefer to keep your Select Case, then just reset the counter after the third run instead of locking it at 3:

    Case 3
        ProcessSelection 3
        gRunCount = 0
    ``
    

    Then the next press starts again at 1.

    If you ever want the number to be remembered even after closing and reopening the document, it is recommended that you store it in a Word document variable instead, because document variables are meant to preserve macro settings between sessions.

    Thank you for your patience in reading, I hope this information has been helpful to you. 


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment."    

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 

    2 people found this answer helpful.

  2. Q&A Assist
    2026-04-27T00:35:50.91+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Change the logic so that the counter is reset after 3, instead of being held at 3. For example:

    Option Explicit
    
    Dim gRunCount As Long
    
    Sub OneButtonMacro()
    
        'Increment run counter each time the macro is pressed
        gRunCount = gRunCount + 1
    
        Select Case gRunCount
            Case 1
                ProcessSelection 1
            Case 2
                ProcessSelection 2
            Case 3
                ProcessSelection 3
            Case Else
                'After third time, reset to 1
                gRunCount = 1
                ProcessSelection 1
        End Select
    
    End Sub
    

    Alternatively, to keep the logic compact and always cycle 1 → 2 → 3 → 1 → 2 → 3, use the Mod operator:

    Option Explicit
    
    Dim gRunCount As Long
    
    Sub OneButtonMacro()
        'Cycle through 1,2,3 repeatedly
        gRunCount = (gRunCount Mod 3) + 1
        ProcessSelection gRunCount
    End Sub
    

    ProcessSelection can remain exactly as written.


    References:

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.