Word A shorter Version

SteveD 625 Reputation points
2026-03-11T00:59:08.5233333+00:00

Hello From Steve

Objective find for example 2:35 PM the second and then selection.MoveUp Unit:=wdLine, Count:=2

Can the below please be shortened

Sub Time_Spacing_Correction()

Dim rng As range

Set rng = ActiveDocument.Content

With rng.Find

.ClearFormatting

.Replacement.ClearFormatting

.Text = "[0-9]{1,}:[0-9]{1,} [A-Z]{1,}"

.Forward = True

.Wrap = wdFindStop

.Format = False

.MatchCase = True

.MatchWholeWord = False

.MatchWildcards = True

End With

With rng.Find

.ClearFormatting

.Replacement.ClearFormatting

.Text = "[0-9]{1,}:[0-9]{1,} [A-Z]{1,}"

.Forward = True

.Wrap = wdFindStop

.Format = False

.MatchCase = True

.MatchWholeWord = False

.MatchWildcards = True

End With

Do While rng.Find.Execute

 rng.Select

 

selection.MoveUp Unit:=wdLine, Count:=2

selection.Delete Unit:=wdCharacter, Count:=3

selection.MoveDown Unit:=wdLine, Count:=1

rng.Collapse wdCollapseEnd

Loop

End Sub

Microsoft 365 and Office | Word | For home | Windows

1 answer

Sort by: Most helpful
  1. AI answer

    2026-03-11T00:59:17.5966667+00:00

    Yes. The duplicated Find setup can be removed and the code can be simplified while keeping the same behavior:

    Sub Time_Spacing_Correction()
    
        Dim rng As Range
        Set rng = ActiveDocument.Content
    
        With rng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = "[0-9]{1,}:[0-9]{1,} [A-Z]{1,}"
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = True
            .MatchWholeWord = False
            .MatchWildcards = True
        End With
    
        Do While rng.Find.Execute
            rng.Select
            Selection.MoveUp Unit:=wdLine, Count:=2
            Selection.Delete Unit:=wdCharacter, Count:=3
            Selection.MoveDown Unit:=wdLine, Count:=1
            rng.Collapse wdCollapseEnd
        Loop
    
    End Sub
    

    The Find configuration is defined once, then reused in the loop with rng.Find.Execute.


    References:

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

    Was this answer helpful?

    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.