Fill a Word table column with successive integers.

Posted on 2015-01-16
Medium Priority
Last Modified: 2016-02-11
I have more than one table in a word document. One of those tables spreads over several pages, and my cursor is in that table in a specific column named “Ix” (for index). I would like a quick and dirty function that will fill that column with consecutive integers, starting in row 2 and running from 1 to the end of the column. That will preserve the current order of the data. If some subsequent editing or sorting mixes up the rows, I can always return the data to its original order by sorting on that “Ix” column.
     I could solve the problem if I knew the table number and column number of the cursor location.
     If the column I want filled is number six in the second table of the document, I could then use something like this:

     Set Tbl = ActiveDocument.Tables(2)
     Set Col = Tbl.Columns(6)
     MaxRows = Tbl.Rows.Count
     For i = 1 To MaxRows - 1
          Col.Cells(i + 1).Range.Text = i
     Next i

     Any suggestions either as to determining the Table number and Column number, or to use something else entirely are most welcome.
     JRA in Priddis, Alberta
Question by:JohnRobinAllen
  • 2
LVL 77

Accepted Solution

GrahamSkan earned 1600 total points
ID: 40554401
Hi JRA. Just use the first items in the Selection:

Set tbl = Selection.Tables(1)
Set col = tbl.Columns(Selection.Cells(1).ColumnIndex)

Open in new window


Assisted Solution

by:Tony Pitt
Tony Pitt earned 400 total points
ID: 40554411
Does this have to be VBA?  If not, I can do it manually ...

Put number 1 in the required column, row 2.  In the required column, row 3, go to Table Tools->Layout ribbon and hit Formula in the data section - it's on the far right, so won't be visible if the window's too narrow.  Change the formula to "=MAX(ABOVE)+1" (without the double quotes) and hit OK.  That cell should now read 2.  Select that value and paste it into the remainder of the column - they'll all read 2!  Select that column and hit F to update the fields, and you should now have the column numbered.

One remaining step: since this is a formula at present, it might be recalculated so that the numbers change if the order of the rows has changed.  To prevent this, copy the column, and paste it into another column as text only.  (I think you should be able to paste it on top of the column you've been working in, but I can't seem to make that work.)


Author Closing Comment

ID: 40555210
Graham Skan solves the problem perfectly. Tony Pitt's suggestion solves the problem without having to use VBA and suggests how I can use Formula in the Tools / Data section. That will be useful for me in the future.
     Thanks to both of you.

Author Comment

ID: 41394413
I apologize for reopening this question closed almost a year ago. The basic problem is to use VBA to fill a Word table with successive integers to preserve the current sorting order in order to return the table to that order after other sorts. The obvious solution is to use a
     For i = 1 to Tbl.rows.count
loop, but Tony Pitt suggested that I could skip the VBA solution and do it manually using a Table formula. I recently wrote that solution in VBA and timed how long it would take compared to the standard method.
     My table currently has 12 columns and 216 rows. Times varied in successive attempts but in general the loop method took 23 seconds to fill the column and the formula method took between 39 and 46 seconds.
     I would prefer it take around 3 or 4 seconds to fill the column, and I was able to achieve that by a third, totally different method. It requires that you first manually fill the column with successive integers. When you sort the table by some other criteria the column will be jumbled. Then, instead of rewriting the contents of the column to record the new sort, you simply sort that column alone without sorting the rest of the table.
     One word of caution: in my version of Word (2013), manually specifying a sort on a single column will not work. It will sort the column but not correctly if it contains numbers. I have not tried sorting on words.
     All three sorts are in the attached file.
     --John Robin (Allen)
       Priddis, Alberta, Canada

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

624 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