Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-09-2017, 01:09 AM
NevilleT NevilleT is offline Nested tables. Count rows Windows 7 64bit Nested tables. Count rows Office 2003
Novice
Nested tables. Count rows
 
Join Date: Mar 2015
Posts: 29
NevilleT is on a distinguished road
Default 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)?
Reply With Quote
  #2  
Old 05-09-2017, 02:54 AM
gmaxey gmaxey is offline Nested tables. Count rows Windows 7 32bit Nested tables. Count rows Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 05-09-2017, 04:04 AM
NevilleT NevilleT is offline Nested tables. Count rows Windows 7 64bit Nested tables. Count rows Office 2003
Novice
Nested tables. Count rows
 
Join Date: Mar 2015
Posts: 29
NevilleT is on a distinguished road
Default

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?
Reply With Quote
  #4  
Old 05-09-2017, 04:35 AM
gmaxey gmaxey is offline Nested tables. Count rows Windows 7 32bit Nested tables. Count rows Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Yes.

Msgbox oTbl.Rows.Count
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 05-09-2017, 05:51 AM
NevilleT NevilleT is offline Nested tables. Count rows Windows 7 64bit Nested tables. Count rows Office 2003
Novice
Nested tables. Count rows
 
Join Date: Mar 2015
Posts: 29
NevilleT is on a distinguished road
Default

So simple! Thanks Greg. When I get the whole thing sorted I will post the result for others with the same issue.
Reply With Quote
  #6  
Old 05-09-2017, 06:11 AM
NevilleT NevilleT is offline Nested tables. Count rows Windows 7 64bit Nested tables. Count rows Office 2003
Novice
Nested tables. Count rows
 
Join Date: Mar 2015
Posts: 29
NevilleT is on a distinguished road
Default

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
Reply With Quote
  #7  
Old 05-09-2017, 07:25 AM
gmaxey gmaxey is offline Nested tables. Count rows Windows 7 32bit Nested tables. Count rows Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 05-10-2017, 12:25 AM
NevilleT NevilleT is offline Nested tables. Count rows Windows 7 64bit Nested tables. Count rows Office 2003
Novice
Nested tables. Count rows
 
Join Date: Mar 2015
Posts: 29
NevilleT is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 05-10-2017, 04:17 AM
gmaxey gmaxey is offline Nested tables. Count rows Windows 7 32bit Nested tables. Count rows Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #10  
Old 05-10-2017, 05:22 AM
NevilleT NevilleT is offline Nested tables. Count rows Windows 7 64bit Nested tables. Count rows Office 2003
Novice
Nested tables. Count rows
 
Join Date: Mar 2015
Posts: 29
NevilleT is on a distinguished road
Default

Thanks Greg. Visited your site and it is a treasure trove.
Reply With Quote
Reply

Tags
nested table

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested tables. Count rows 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
Nested tables. Count rows 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

Other Forums: Access Forums

All times are GMT -7. The time now is 09:19 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft