View Single Post
 
Old 04-18-2022, 07:38 PM
Peterson Peterson is offline Windows 10 Office 2019
Competent Performer
 
Join Date: Jan 2017
Posts: 143
Peterson is on a distinguished road
Default

It's not clear what you want to to with the numerals, but the following code will remove the gaps in the text and remove the empty paragraphs preceding the numerals:
Code:
Sub CleanUpBlankLines() 

' Finds paragraph marks and spaces between broken paragraphs and removes them
' Finds empty paragraphs preceding numerals and removes them, too

    Application.ScreenUpdating = False
    
    Dim oRange As Range
         
    Set oRange = ActiveDocument.Range

    With oRange.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = True
        
        ' Remove gaps in text:
        .Text = "([!0-9] )([^13 ]{1,})([!0-9])"
        .Replacement.Text = "\1\3"
        .Execute Replace:=wdReplaceAll
        
        ' Remove spaces between empty paragraphs, in preparation
        ' to remove paragraphs preceding numbers:
        .Text = "(^13)( {1,})(^13)"
        .Replacement.Text = "\1\3"
        .Execute Replace:=wdReplaceAll
        
        ' Remove empty paragraphs preceding numbers:
        .Text = "([^13]{2,})([0-9])"
        .Replacement.Text = "^13\2"
        .Execute Replace:=wdReplaceAll
    
    End With
    
    With oRange.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = False
    End With
    
     Set oRange = Nothing

     Application.ScreenUpdating = True

 End Sub

Last edited by Peterson; 04-19-2022 at 08:29 AM.
Reply With Quote