Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-17-2019, 03:10 AM
simons simons is offline Design macro insert number table rows Windows 10 Design macro insert number table rows Office 2019
Novice
Design macro insert number table rows
 
Join Date: Sep 2019
Posts: 4
simons is on a distinguished road
Default Design macro insert number table rows

Hi there,

In a lot of documents I need to number each cell of the first column of each row of a table. Does someone have a macro or can someone creat a macro that does the following:

For each table with specific table.style: "xxxx"
Insert number based on the list of the heading and row of the cell.
For example:

Attached Images
File Type: png TableIDnumber.png (2.3 KB, 16 views)
Reply With Quote
  #2  
Old 09-18-2019, 06:16 AM
gmaxey gmaxey is offline Design macro insert number table rows Windows 10 Design macro insert number table 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

Something like this might get you close:


Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Dim oRng As Range
Dim strList As String
Dim oRow As Row
Dim lngRow As Long
  For Each oTbl In ActiveDocument.Tables
    If oTbl.Style = "Table Grid" Then
      Set oRng = oTbl.Range
      oRng.Collapse wdCollapseStart
      oRng.Move wdParagraph, -1
      If Not oRng.Paragraphs(1).OutlineLevel < 10 Then
        Do
          oRng.Move wdParagraph, -1
        Loop Until oRng.Paragraphs(1).OutlineLevel < 10
      End If
    End If
    strList = oRng.Paragraphs(1).Range.ListFormat.ListString
    lngRow = 0
    For Each oRow In oTbl.Rows
      lngRow = lngRow + 1
      oRow.Cells(1).Range.Text = strList & "." & lngRow
    Next oRow
  Next oTbl
lbl_Exit:
  Exit Sub
  
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 09-18-2019, 06:48 AM
simons simons is offline Design macro insert number table rows Windows 10 Design macro insert number table rows Office 2019
Novice
Design macro insert number table rows
 
Join Date: Sep 2019
Posts: 4
simons is on a distinguished road
Default

Hi, Thanks for your effort

It gives an error at
Code:
    strList = oRng.Paragraphs(1).Range.ListFormat.ListString
Run-time error '91'
Object variable or With block variable not set
Reply With Quote
  #4  
Old 09-18-2019, 06:57 AM
gmaxey gmaxey is offline Design macro insert number table rows Windows 10 Design macro insert number table 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

Would have to see a sample document with a couple of tables and the preceding heading text.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 09-18-2019, 07:13 AM
simons simons is offline Design macro insert number table rows Windows 10 Design macro insert number table rows Office 2019
Novice
Design macro insert number table rows
 
Join Date: Sep 2019
Posts: 4
simons is on a distinguished road
Default

Please find attached document
Attached Files
File Type: docx Table ID test.docx (14.3 KB, 7 views)
Reply With Quote
  #6  
Old 09-18-2019, 11:52 AM
kilroy kilroy is offline Design macro insert number table rows Windows 10 Design macro insert number table rows Office 2016
Competent Performer
 
Join Date: Sep 2016
Location: Southern Ontario
Posts: 118
kilroy is on a distinguished road
Default

That error is from the style line.


Change
If oTbl.Style = "Table Grid" Then


to
If oTbl.Style = "Protix Table" Then


I can't figure out how to get it to skip the first row. Right now it overwrites the "ID" in row one.
Reply With Quote
  #7  
Old 09-18-2019, 12:28 PM
gmaxey gmaxey is offline Design macro insert number table rows Windows 10 Design macro insert number table 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

Code:
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Dim oRng As Range
Dim strList As String
Dim oRow As Row
Dim lngRow As Long
  For Each oTbl In ActiveDocument.Tables
    If oTbl.Style = "Protix Table" Then
      Set oRng = oTbl.Range
      oRng.Collapse wdCollapseStart
      oRng.Move wdParagraph, -1
      If Not oRng.Paragraphs(1).OutlineLevel < 10 Then
        Do
          oRng.Move wdParagraph, -1
        Loop Until oRng.Paragraphs(1).OutlineLevel < 10
      End If
    End If
    strList = oRng.Paragraphs(1).Range.ListFormat.ListString
    lngRow = 0
    For Each oRow In oTbl.Rows
      If oRow.Index > 1 Then
        lngRow = lngRow + 1
        oRow.Cells(1).Range.Text = strList & "." & lngRow
      End If
    Next oRow
  Next oTbl
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #8  
Old 09-20-2019, 12:05 AM
simons simons is offline Design macro insert number table rows Windows 10 Design macro insert number table rows Office 2019
Novice
Design macro insert number table rows
 
Join Date: Sep 2019
Posts: 4
simons is on a distinguished road
Default

It works up until I have other tables in the document aswell
If there is another table style used before the 'protix table' then it doesn't work
If there is another table style used after a 'protix table' then it also gives that table an ID number.
Attached Files
File Type: docx Table ID test.docx (15.2 KB, 4 views)
Reply With Quote
Reply

Tags
insert number

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design macro insert number table rows Design Macro or Code to Delete Table Rows When Null/Blank/Empty Upon Doc Close/Save aaghd72 Word VBA 8 02-18-2019 11:22 AM
How to find number of coma and then add that number of rows in word using macro? PRA007 Word VBA 7 05-27-2015 10:45 PM
Creating a table for a variable number of rows OllieOnline Mail Merge 1 03-27-2013 02:48 PM
Using macro to add variable number of rows to a protected word table Julia Word Tables 1 01-09-2013 06:04 AM
Design macro insert number table rows Fixing number or rows in a table burnsie Word 2 07-12-2011 02:59 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:02 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