Link to home
Start Free TrialLog in
Avatar of JohnRobinAllen
JohnRobinAllenFlag for Canada

asked on

Fill a Word table column with successive integers.

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
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JohnRobinAllen

ASKER

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.
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
Three-Subs.docx