0

(I have avidly searched the forum, but the only similar 'replace' questions I could find were related to Python, Java etc, and not VBA)

  • I have a table within a MS Word (2010) document (it has two columns but only the second column has text in)
  • Some cells in the second column have one line of text and NO paragraph mark
  • Other cells in the second column have two lines of text and TWO paragraph marks (^p)
  • There is no regular pattern between these two types of cells
  • Where there is a second paragraph mark, this always occurs directly before the end-of-cell marker

I really need a macro to remove (replace with " ") this second (=final) paragraph mark from each cell where it occurs, but to ignore any first paragraph marks within the cells.

I would be extremely grateful if someone out there has the time and inclination to help me with this macro. As you might have guessed I have little experience in VBA despite attempting to give myself a crash course: I am more used to recording macros which is not an option in this more intricate case.

My hopes were raised when Google found me this- which looks like it could be adaptable http://www.vbaexpress.com/kb/getarticle.php?kb_id=334; but I have no idea how to tweak it to only replace a SECOND (/final) occurrence of ^p.

Thank you in advance.

1 Answer 1

1

How about:

Dim tbl As Table
Dim c As Cell


For Each tbl In ActiveDocument.Tables
    For Each c In tbl.Columns(2).Cells
        'The end of a cell without a carriage return is vbCr & Chr(7)
        If Right(c.Range.Text, 3) = vbCr & vbCr & Chr(7) Then
            c.Range.Text = Mid(c.Range.Text, 1, Len(c.Range.Text) - 3) & Chr(7)
        End If
    Next
Next
1
  • First and foremost- THANK YOU! I am so much closer now than yesterday. The problem now is that I am losing all formatting in the macro-ed cells (the formatting varies between different lines of the same cell), and I realise this is because we are removing the end-of-cell marker. Is there anyway, instead of removing the final 3 characters [Len(c.Range.Text) - 3], we can remove the third-to-last character only? (length of cell text varies...) Many thanks again. (I have researched the forum/google and cannot find anything solid myself.)
    – Beck0York
    Aug 29, 2012 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.