Link to home
Start Free TrialLog in
Avatar of Andreas Hermle
Andreas HermleFlag for Germany

asked on

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

Avatar of Phillip Burton
Phillip Burton

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.
Avatar of Andreas Hermle

ASKER

Hi Philipp,

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

Regards, Andreas
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
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
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.
Hi Philipp

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

Regards, Andreas
Can you please post your document.
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Great job, as always :-)