Run a macro code only on certain tables not all

Dear Experts

Line 23 of the below code loops thru all the tables of the current document subsequently creating bookmarks in all the tables. The trouble is ...
... this time the macro should only run on table 5, 13, 19, 23, 27, 44, 54, 73. And in subsequent cases there could be totally different tables that need to be worked on.

I got no idea how to re-write this code to satisfy this requirement.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas


Sub Add_Table_Bookmarks()

    Dim tbl As Table
    Dim b As Integer
    Dim rng As Range
    Dim Bookmark1 As Variant
    Dim Bookmark2 As Variant
    
    
         If MsgBox("Would you like to create bookmarks in certain cells of all the tables?" & vbCrLf & vbCrLf & _
 "These bookmarks form the basis for TOC items!" & vbCrLf & _
 "Normally the MOH-Codes and Set Descriptions are the ones to be bookmarked", vbYesNo + vbQuestion, "Create bookmarks in table!") = vbNo Then
        Exit Sub
        End If

Bookmark1 = InputBox("Please indicate the first cell to be bookmarked!", "first cell to be bookmarked")
     If Bookmark1 = "" Then Exit Sub
Bookmark2 = InputBox("Please indicate the second to be bookmarked!", "second cell to be bookmarked")
     If Bookmark2 = "" Then Exit Sub
    
    b = 1
    With ActiveDocument
        For Each tbl In .Tables ' new requirement: the macro should only run on table 5, 13, 19, 23, 27, 44, 54, 73
           Set rng = tbl.Cell(2, Bookmark1).Range 'first cell
            rng.MoveEnd wdCharacter, -1
            .Bookmarks.Add ("bookmark_" & b), rng
            
            Set rng = tbl.Cell(2, Bookmark2).Range 'fourth cell
            rng.MoveEnd wdCharacter, -1
            .Bookmarks.Add ("bookmark_" & b + 1), rng
            b = b + 2
        Next tbl
    End With
    
End Sub

Open in new window

Andreas HermleTeam leaderAsked:
Who is Participating?
 
Rgonzo1971Connect With a Mentor Commented:
Hi,

pls try

   
With ActiveDocument
   Set a = ActiveDocument.Tables
        For Idx = 1 To .Tables.Count ' new requirement: the macro should only run on table 5, 13, 19, 23, 27, 44, 54, 73
            Select Case Idx
                Case 5, 13, 19, 23, 27, 44, 54, 73
            
                    Set rng = .Tables(Idx).Cell(2, Bookmark1).Range 'first cell
                    rng.MoveEnd wdCharacter, -1
                    .Bookmarks.Add ("bookmark_" & b), rng
            
                    Set rng = .Tables(Idx).Cell(2, Bookmark2).Range 'fourth cell
                    rng.MoveEnd wdCharacter, -1
                    .Bookmarks.Add ("bookmark_" & b + 1), rng
                    b = b + 2
            End Select
        Next
    End With

Open in new window

Regards
0
 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
In between lines 23 and 24 insert

Select Case tbl.Name
Case "5", "13",     etc.

and after line 31

End Select

The CASE line will need adjusting based on the table names.
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Philipp,

thank you very much for your swift help. I am afraid to tell you that there is no 'tbl.Name'

Regards, Andreas
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
You're right.

How do you know that they are tables 5, 13 etc.

If you mean that they are the 5th table in the document, then try this:

In between lines 23 and 24 insert

Select Case (b+1)/2
Case 5, 13,     etc.

and after line 31

End Select
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Philipp, thank you very much for your swift help, yes you are right I refer to the 5th or 9th or 13th etc table in the current document.

I am not quite sure about ...
Select Case (b+1)/2

but of course I will give it a try.

Regards, Andreas
0
 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Have a look at line 21:

    b = 1

and line 31:

            b = b + 2

This means that, for the first table, b=1, for the second b=3 etc. So (b+1)/2 will equal 1 for the first table, 2 for the second etc.
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Philipp

I am afraid to tell you that nothing happens if I run the revised code.

Regards, Andreas
0
 
Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Can you please post your document.
0
 
Andreas HermleTeam leaderAuthor Commented:
Hi Rgonzo,

great, I wanted it exactly this way. Thank you very much for your professional help.

Philipp, sorry, but Rgonzo was quicker but anyway, thank you very much for your great help. I really appreciate it.

Regards, Andreas
0
 
Andreas HermleTeam leaderAuthor Commented:
Great job, as always :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.