Word 2010:  (Loop) How to add new row to table based on cell values matches

Posted on 2014-08-26
Last Modified: 2014-08-26

I'm trying to loop through a table in word and add a new row between two non matching cells (cells 1 of row x & cells 1 of row x+1) as the non matching instances are found.

I know the below is crude (and doesn't work:0)), but if you could help that'd be great

Dim x As Long
Dim TableY As Table
x = 1
Set TableY = Selection.Tables(1).Cell(x, 1)
Do Until TableY.Cell(x, 1) <> TableY.Cell(x + 1, 1)
        Selection.InsertRowsAbove 1
        x = x + 1

Question by:DannyJOsborne
    LVL 47

    Accepted Solution


    pls try

    Sub Macro1()
    Set TableY = Selection.Tables(1)
    For Idx = TableY.Rows.Count To 2 Step -1
        If TableY.Cell(Idx, 1).Range.Text <> TableY.Cell(Idx - 1, 1).Range.Text Then
            TableY.Cell(Idx, 1).Select
        End If
    End Sub

    Open in new window


    Author Closing Comment

    Spot on first time!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Nice table. Huge mess. Maybe this was something you created way back before you figured out tabs or a document you received from someone else. Either way, using the spacebar to separate the columns resulted in a mess. Trying to convert text to t…
    This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
    This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now