How to run code in Each table in the document instead of just the First one?

Snaybot

New Member
Joined
Apr 28, 2015
Messages
40
Code:
Sub Macro7()'
' GBM2 Macro
'


Dim i As Long, StrTxt As String
' Setting the variable
With ActiveDocument.Tables(1)
' Active Documents in the table
  For i = .Rows.Count To 2 Step -1
' Counts the rows by 1 down
    If .Rows(i - 1).Cells.Count > 1 Then
' Rows will count by one cell
      If InStr(.Cell(i, 2).Range.Text, "Ensure S") = 1 Then
' if the second column starts with "Ensure S" you know what I mean the first part of the procedure in the step then
        StrTxt = Split(.Cell(i, 2).Range.Text, " ")(1)
' Until the End or until the space is found after "Ensure S"
        .Rows.Add BeforeRow:=.Rows(i)
' Add an extra row for the Grey Bar
        With .Rows(i)
          .Cells.Merge
          'Merge those Cells!
          .Shading.BackgroundPatternColor = -603930625
          'Grey those cells!
          .Range.Style = wdStyleNormal
          'wdStyleNormal is the normal font template, for me which is Times new roman 12 for others it could be arial or calabri based on the template
          .Range.ParagraphFormat.KeepWithNext = wdToggle
          'Keep with next
          .Range.Text = "Step " & StrTxt
          'add "Step" Text infront of the step number
          .Range.Rows.Height = 18
          'Height is configurable
          .Range.Bold = wdToggle
          'Bold the Grey Bar text
          .Range.Font.Size = 10
          'Now shrink the text
        End With
      End If
    Else
      i = i - 1
      ' Makes the program not crash <- Suprisingly the hardest part lol
    End If
  Next
End With
'
End Sub

I have this Code How do I run it for every table in the Document rather than only the first table?

Crossposted at:
http://www.excelforum.com/excel-pro...nstead-of-just-the-first-one.html#post4236580
http://www.msofficeforums.com/excel...de-each-table-document-instead.html#post91159
 
Last edited:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Obviously, you'd need to loop through the tables. For example
Code:
Dim Tbl As Table
For Each Tbl In ActiveDocument.Tables
  With Tbl
    'Process the table here
  End With
Next
 
Upvote 0
How would I do that?


Do I just copy and paste my code inside your With statement?


I have tried multiple times. Im pretty novice at this and still learning


I wish there was a select all Tables haha
 
Upvote 0
See my reply at MS Office Forums. I don't propose to post the same replies in every one of your cross-posts. As Rule #10 says:
We prefer that members do not cross-post questions to other forums
By cross-posting you may actually diminish the chances of getting a response, since people are often disinclined to chase cross-post discussions to see whether the matter has already been addressed. You also risk getting replies that are at variance with one another.
 
Upvote 0
will do sorry, Macropod

Ill stick with the one youve previously replied

Ahh Im having an error, here.



Run-Time error '5991'


Cannot access individual row in this collection because the table has vertically merged cells


Any way to skip these table


Or start at row 3 on each table?

I will stick to one from now on
 
Upvote 0

Forum statistics

Threads
1,215,336
Messages
6,124,328
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top