Solved

Fill a Word table column with successive integers.

Posted on 2015-01-16
4
122 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now