Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6472623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:22:04+00:00 2026-05-25T06:22:04+00:00

I wish to be able to run a VBA module which manipulates the table

  • 0

I wish to be able to run a VBA module which manipulates the table that I’m currently in (i.e., the cursor is somewhere within that table). The VBA code will perform an identical operation on each table that you’re in when you run it.

So, for example, let’s say I have a module which needed to bold the top row of each table (the headings). It would need to locate the table object (called whatever) that you’re currently in so that it could manipulate whatever.rows(0).

How can I get the table object from the cursor position? I also need to detect if I’m not in a table and do nothing (or raise an error dialog).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:22:04+00:00Added an answer on May 25, 2026 at 6:22 am

    The VBA subroutine at the bottom of this answer shows how to do this.

    It uses the current selection, collapsing it to the starting point first so as to not have to worry about multi-segment selections:

    Selection.Collapse Direction:=wdCollapseStart
    

    It then checks that selection to ensure it’s inside a table

        If Not Selection.Information(wdWithInTable) Then
            MsgBox "Can only run this within a table"
            Exit Sub
        End If
    

    The table is then accessible by referring to Selection.Tables(1).


    The code below was a simple proof of concept which simply toggled each of the starting cells in each row of the table to either insert or delete a vertical bar marker.

    Sub VertBar()
        ' Collapse the range to start so as to not have to deal with '
        ' multi-segment ranges. Then check to make sure cursor is '
        ' within a table. '
        Selection.Collapse Direction:=wdCollapseStart
        If Not Selection.Information(wdWithInTable) Then
            MsgBox "Can only run this within a table"
            Exit Sub
        End If
    
        ' Process every row in the current table. '
        Dim row As Integer
        Dim rng As Range
    
        For row = 1 To Selection.Tables(1).Rows.Count
            ' Get the range for the leftmost cell. '
            Set rng = Selection.Tables(1).Rows(row).Cells(1).Range
    
            ' For each, toggle text in leftmost cell. '
            If Left(rng.Text, 2) = "| " Then
                ' Change range to first two characters and delete them. '
                rng.Collapse Direction:=wdCollapseStart
                rng.MoveEnd Unit:=wdCharacter, Count:=2
                rng.Delete
            Else
                ' Just insert the vertical bar. '
                rng.InsertBefore ("| ")
            End If
        Next
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to code a little service where I will be able to send
As the topic suggests I wish to be able to pass table names as
I have a 3d object that I wish to be able to rotate around
I wish to be able to change the table a class is mapped to
I wish to be able to generate URL variables like this: http://example.com/195yq http://example.com/195yp http://example.com/195yg
I wish to be able to record, in real time, the activity of a
I wish Subversion had a better way of moving tags. The only way that
I wish to implement my software on a shareware basis, so that the user
I wish to develop a client-server application in .NET that functions as follows: Clients
Let's say I have an existing application written in Java which I wish to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.