Share via

Word A cleaner script.

Anonymous
2023-08-14T01:27:12+00:00

Hello from Steve

Is their a cleaner approach to the below please.

From this

   9th:   9 FLOWER MOON         

10th:  10 HANALEI STAR

To this

      9 FLOWER MOON          

    10 HANALEI STAR

Sub Delete_Starters()

Dim rng As Range 

Selection.Find.ClearFormatting 

With Selection.Find 

    .Text = "[0-9]{1,}[a-z]{1,}:" 

    .Replacement.Text = " " 

    .Forward = True 

    .Wrap = wdFindStop 

    .Format = True 

    .MatchCase = False 

    .MatchWholeWord = False 

    .MatchAllWordForms = False 

    .MatchSoundsLike = False 

    .MatchWildcards = True 

Do While .Execute 

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

Selection.TypeText Text:="     " 

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

Selection.HomeKey Unit:=wdLine 

Loop

End With 

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2023-08-14T02:38:10+00:00

Hello

I’m Adeyemi and I’d be happy to help you with your question.

The script you provided is a VBA macro that removes the text before the colon and replaces it with spaces to align the text. Here's a cleaner version of the script that does the same thing:

Sub Delete\_Starters()
    Dim rng As Range
    Set rng = ActiveDocument.Range
    With rng. Find
        . ClearFormatting
        . Text = "[0-9]{1,}[a-z]{1,}:"
        . Replacement.Text = "     "
        . Forward = True
        . Wrap = wdFindStop
        . Format = False
        . MatchCase = False
        . MatchWholeWord = False
        . MatchAllWordForms = False
        . MatchSoundsLike = False
        . MatchWildcards = True
        . Execute Replace:=wdReplaceAll
    End With
End Sub

This version of the script uses the Replace parameter of the Execute method to replace all occurrences of the text matching the search pattern with spaces. This eliminates the need for a loop and makes the code shorter and easier to read.

I hope this helps!

Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below

Regards Adeyemi

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

8 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-08-14T09:17:44+00:00

    I'm sorry to hear that the revised script is not finding anything. It's possible that the search pattern in the script is not matching the text in your document. Could you please provide an example of the text that you're trying to modify with the script? This will help me better understand your issue and provide a more accurate response.

    Regards Adeyemi

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-08-14T04:24:24+00:00

    Hello Adeyemi

    This [0-9]{1,} ? [a-z] {1,}: is not finding anything

    I am Getting "No results found".

    I know Adeyemi you are close.

    All the Best

    Steve

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-08-14T03:34:48+00:00

    I'm glad that the revised script was able to help you partially achieve your goal. However, it seems that the script is still missing some double numerals. To fix this issue, you can try modifying the search pattern in the script to include an optional space character between the digits and the letters. Here's a revised version of the script that should be able to handle double numerals with or without a space between them:

    Sub Delete\_Starters()
        Dim rng As Range
        Set rng = ActiveDocument.Range
        With rng. Find
            . ClearFormatting
            . Text = "[0-9]{1,} ? [a-z] {1,}:"
            . Replacement.Text = "     "
            . Forward = True
            . Wrap = wdFindStop
            . Format = False
            . MatchCase = False
            . MatchWholeWord = False
            . MatchAllWordForms = False
            . MatchSoundsLike = False
            . MatchWildcards = True
            . Execute Replace:=wdReplaceAll
        End With
    End Sub
    

    In this revised version of the script, I added a space followed by a question mark after the [0-9]{1,} part of the search pattern. This tells the script to look for an optional space character between the digits and the letters.

    Regards Adeyemi

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-08-14T03:30:11+00:00

    Hello Adeyemi

    I thank you for the quick reply

    The result is it does only some Double numerals but missus others as an example below.

    I am happy what you have acheived, but is it possible please to sort the missed ones.

    All the Best Steve

               9 FLOWER MOON         9      

             10 HANALEI STAR       10

    Was this answer helpful?

    0 comments No comments