• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 394
  • Last Modified:

How do I create a macro that removes empty carriage returns from a Word table cell?

I'm a technical writer and often edit people's Word documents. A common formatting issue is that people place extra carriage returns in their tables. Word has an excellent search/replace facility, but it doesn't have a code for finding the end-of-table-cell character. So, I can remove all double carriage returns (¶¶, or ^p^p) in a cell, but can't remove that final ¶ before the end of the cell. I'd like to create a macro to find and replace "¶ followed by an end-of-table-cell character" with just the end-of-table-cell character. I know this has got to be an extremely simple bit of code but I'm not familiar enough with VBA yet to know what to do. Thanks in advance for any suggestions.
0
calyx_teren
Asked:
calyx_teren
1 Solution
 
GrahamSkanRetiredCommented:
I think this will help you.
Sub TidyCells()
    Dim cl As Cell
    Dim tbl As Table
    Dim rng As Range
    
    For Each tbl In ActiveDocument.Tables
        For Each cl In tbl.Range.Cells
            Set rng = cl.Range
            rng.MoveEnd wdCharacter, -1 'moves back from cell formatting character
            Do While Right$(rng.Text, 1) = vbCr 'move back over paragraph marks
                rng.MoveEnd wdCharacter, -1
            Loop
            cl.Range.Text = rng.Text
        Next cl
    Next tbl
End Sub

Open in new window

0
 
calyx_terenAuthor Commented:
Thank you very much. This will save me enormous amounts of tedious work, and I learned more about VBA in the bargain. Much appreciated.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now