Thread: [Solved] Counting items in the table
View Single Post
 
Old 08-25-2017, 10:17 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,467
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

If you save the document in the docm format, the following macro added to the documents 'ThisDocument' code module will update the second table any time you exit a content control in the first table.
Code:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Application.ScreenUpdating = False
Dim Tbl1 As Table, Tbl2 As Table, r As Long, i As Long, j As Long
If CCtrl.Title Like "Name#" Then
  Set Tbl1 = ActiveDocument.Tables(1)
  Set Tbl2 = ActiveDocument.Tables(2)
  With Tbl2
    For r = 2 To .Rows.Count
      .Cell(r, 2).Range.Text = 0
    Next
  End With
  With Tbl1
    For r = 1 To .Rows.Count
      With .Cell(r, 1).Range.ContentControls(1).Range
        If .Text Like "Position #" Then
          i = CLng(Split(.Text, " ")(1)) + 1
          j = CLng(Split(Tbl2.Cell(i, 2).Range.Text, vbCr)(0))
          Tbl2.Cell(i, 2).Range.Text = j + 1
        End If
      End With
    Next
  End With
End If
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote