[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Word VBA - Selecting Tables

Posted on 2012-08-21
Medium Priority
Last Modified: 2012-08-21
Hi Experts,

Does anyone know how I can select a table in a Word document based on the value of the first cell? I.e. Instead of using ActiveDocument.Tables(2).Select, I could replace the value of the direct reference to the table with the value of the first cell in the desired table, such as the title of the table which is a unique value.


Question by:MrDavidThorn
  • 2
LVL 76

Accepted Solution

GrahamSkan earned 1600 total points
ID: 38315726
Just step through  the tables and check the cell particular cell contents.

Sub SelectTableByText()
    Dim tbl As Table
    Dim strText As String
    strText = "My Text"
    For Each tbl In ActiveDocument.Tables
        If GetCellText(tbl.Range.cells(1)) = strText Then
            Exit For 'to select first eligible table. else last such table will be selected
        End If
    Next tbl
End Sub

Function GetCellText(cl As Cell) As String
    Dim rng As Range
    Set rng = cl.Range
    rng.MoveEnd wdCharacter, -1
    GetCellText = rng.Text
End Function

Open in new window


Author Comment

ID: 38315734
Excellent - thank you

Author Closing Comment

ID: 38315735
Thank you

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many companies are making the switch from Microsoft to Google Apps (https://www.google.com/work/apps/business/). Use this article to learn more about what Google Apps has to offer and to help if you’re planning on migrating to Google Apps. It is …
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
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.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

872 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