Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Fill a Word table column with successive integers.

Posted on 2015-01-16
4
140 Views
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
0
Comment
Question by:JohnRobinAllen
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 400 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

0
 
LVL 4

Assisted Solution

by:Tony Pitt
Tony Pitt earned 100 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.)

/T
0
 

Author Closing Comment

by:JohnRobinAllen
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.
0
 

Author Comment

by:JohnRobinAllen
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
Three-Subs.docx
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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