[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Fill a Word table column with successive integers.

Posted on 2015-01-16
4
Medium Priority
?
192 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 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

0
 
LVL 4

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.)

/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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

831 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