Split big table several times on certain criterion

Posted on 2012-08-15
Last Modified: 2012-08-16
Dear Experts:

I got a big table spanning over a lot of pages.

I would like to achieve the following using a VBA macro:

The macro should perform the following tasks:

... to look for an entry in column A and ...
... whenever it hits an entry on Column A select that row and SPlIT the table
... since there are numerous enries in Column A of the table several splits are to be performed
... the table splittings automatically result in blank paragraphs.
... now the macro should insert page breaks (Ctrl + Enter) on these blank paragraphs.

Help is much appreciated. Thank you very much in advance.

I have attached a sample file for your convenience with before macro / after macro

Regards, Andreas

Question by:AndreasHermle
    LVL 22

    Accepted Solution

    See if this macro works for you:

    Sub SplitTable()
    Dim t As Table
    Dim r As Integer
    Dim tc As Integer
    Dim c As String
    tc = 1
    Set t = ActiveDocument.Tables(1)
      For r = 1 To t.Rows.Count
        c = Len(t.Cell(r, 1).Range.Text)
          If c > 2 Then
            t.Cell(r, 1).Select
              Selection.InsertBreak Type:=wdPageBreak
                tc = tc + 1
              Set t = ActiveDocument.Tables(tc)
            r = 2
          End If
      Next r
    End Sub

    Open in new window


    Author Closing Comment

    Hi flyster,

    great coding! Works like a charm, I wanted it exactly this way. Great!

    Thank you very much for your professional job.

    Regards, Andreas
    LVL 22

    Expert Comment


    Thanks for the kind words. I'm happy it worked the way you wanted.


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
    This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
    This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
    Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now