#1
|
|||
|
|||
Nested tables. Count rows
I have a series of tables with nested tables. There may be more than one nested table in each primary table cell.
I want a procedure that counts the number of rows in whichever table is the current selection (assuming it is a nested table). I know how to verify it is nested, so that is not the problem. Once I know the number of rows, I want to number from cell(2,1) - below the header - by entering text "1. ", movedown, "2. " etc. Think I can do that if I can identify the nested table. Problem is that a primary table might have two or three nested tables in one cell. If the cursor is in nested table 2, I can use: intRowNos = Selection.Tables(1).Tables(2).Rows.Count but how do I cater for the fact it could be in Tables(2) or (1) or even (3)? |
#2
|
|||
|
|||
Sub ScratchMacro()
Dim oTbl As Table 'A basic Word macro coded by Greg Maxey Set oTbl = Selection.Tables(1) lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Before I do my worst with this, Greg, is oTbl the nested table? How do i then use it to find the number of rows in the table?
|
#4
|
|||
|
|||
Yes.
Msgbox oTbl.Rows.Count |
#5
|
|||
|
|||
So simple! Thanks Greg. When I get the whole thing sorted I will post the result for others with the same issue.
|
#6
|
|||
|
|||
This is the final code.
Sub subNumberTableColumns() Dim objTable As Table Dim intRowNo As Integer Dim intCounter As Integer On Error GoTo Error_subNumberTableColumns '--------------------------------------------------------------- ' Must be in a table to delete it If funIsTable = False Then MsgBox "You must be in a table to number the table.", vbCritical, "Number Block" GoTo Exit_subNumberTableColumns Else ' In a table Set objTable = Selection.Tables(1) intRowNo = objTable.Rows.Count ' Rows in the table objTable.Cell(2, 1).Select ' Go to the cell below the header '--------------------------------------------------------------- ' Enter numbers in each row For intCounter = 1 To intRowNo - 1 ' Number of rows minus the header Selection.HomeKey Unit:=wdLine ' Start of the line Selection.Range.Text = intCounter & ". " Selection.MoveDown Next intCounter End If Exit_subNumberTableColumns: On Error GoTo 0 Exit Sub Error_subNumberTableColumns: MsgBox "An unexpected situation arose in your program." & vbCrLf & _ "Please write down the following details:" & vbCrLf & vbCrLf & _ "Module Name: " & "(MODULE_NAME)" & vbCrLf & _ "Type: VBA Document: " & vbCrLf & _ "Calling Procedure: " & "(PROCEDURE_NAME}" & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Descritption: " & Err.Description & vbCrLf & vbCrLf & _ "Resume Exit_" & "(PROCEDURE_NAME)" & vbCrLf & _ "Resume" End Sub Thanks Greg |
#7
|
|||
|
|||
How about:
Code:
Sub subNumberTableColumns() Dim oTbl As Table Dim lngIndex As Integer Dim oRng As Range On Error GoTo Error_subNumberTableColumns If Selection.Information(wdWithInTable) Then Set oTbl = Selection.Tables(1) For lngIndex = 1 To oTbl.Rows.Count - 1 Set oRng = oTbl.Cell(lngIndex + 1, 1).Range oRng.InsertBefore lngIndex & ". " Next lngIndex Else MsgBox "You must be in a table to number the table.", vbCritical, "Number Block" End If lbl_Exit: Exit Sub Error_subNumberTableColumns: MsgBox "An unexpected situation arose in your program." & vbCrLf & _ "Please write down the following details:" & vbCrLf & vbCrLf & _ "Module Name: " & "(MODULE_NAME)" & vbCrLf & _ "Type: VBA Document: " & vbCrLf & _ "Calling Procedure: " & "(PROCEDURE_NAME}" & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Descritption: " & Err.Description & vbCrLf & vbCrLf & _ "Resume Exit_" & "(PROCEDURE_NAME)" Resume lbl_Exit End Sub |
#8
|
|||
|
|||
Hi Greg
Thanks for your help. I learned a few things aside from the questions I asked. I had created a function to determine if I was in a table using Selection.Information(wdWithInTable) so just use the function over and over. I have about four or five functions to determine if the selection is a table, text, a line etc. Numbering a table is about one of twenty custom functions I am building. I have used vba extensively in Access and some in MS Project but am new to Word. I have read recently advice not to use selection unless you have to. Not sure why. Maybe you could point me to some information as to why to avoid selection. One thing I have to work out is how to tell if I am in nested table, but will work on that. I am sure I have seen something about identifying a nested table. Will look at your site too. You obviously know Word vba inside out. Thanks again. |
#9
|
|||
|
|||
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey If Selection.Tables(1).NestingLevel > 1 Then MsgBox "Your in a nested table" lbl_Exit: Exit Sub End Sub There is a little on ranges of selection here in the heading "Learn to love ranges" http://gregmaxey.com/word_tip_pages/vba_basics.html |
#10
|
|||
|
|||
Thanks Greg. Visited your site and it is a treasure trove.
|
Tags |
nested table |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Getting the Count formula to count all rows | Jennifer Murphy | Word Tables | 11 | 08-23-2016 09:37 PM |
simple way to count rows in a table? | bkcell | Word | 4 | 03-09-2015 09:06 PM |
copying nested if over multiple rows where one value stays fixed | charles_cat | Excel | 1 | 01-23-2015 01:30 AM |
Count rows and add blank rows accordingly | Hoochtheseal | Word VBA | 1 | 01-29-2013 09:23 PM |
Nested vlookup with varable tables! | Dave Jones | Excel | 0 | 08-30-2012 09:15 AM |