Share via

Word activates Macro

SteveD 555 Reputation points
2026-04-26T23:04:25.3066667+00:00

Hello from Steve

Objective please is to Activate Second Selection and Activate Third selection using a single macro

Meaning push the macro for Top Selection then I will place the cursor for the second selection

and push the single macro to actitivate the second selection

I will place the cursor for the third selection

and finally push the single macro to actitivate the third selection

Sub Top_Selection()

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:="1"

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
```End Sub

Sub Second_Selections()

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:="2"

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


Sub Third_Selection()

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:="3"

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

Answer accepted by question author

AI answer

2026-04-26T23:04:40.5433333+00:00

Use one macro that keeps track of how many times it has been run in the current session and calls a common routine with the correct number. For example, put this in a standard module:

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

End Sub

Usage:

  1. Assign OneButtonMacro to a button or keyboard shortcut (for example via the Record Macro dialog and Quick Access Toolbar customization).
  2. Place the cursor for the first location and run OneButtonMacro → it inserts and formats 1.
  3. Move the cursor to the second location and run OneButtonMacro again → it inserts and formats 2.
  4. Move the cursor to the third location and run OneButtonMacro again → it inserts and formats 3.

The shared ProcessSelection routine contains the logic from the three original macros; the only difference is the number inserted, passed as n.


References:

AI-generated content may be incorrect. Read our transparency notes for more information.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most 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.