Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Fill a Word table column with successive integers.

Posted on 2015-01-16
4
Medium Priority
?
177 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

610 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