Solved

Run a macro code only on certain tables not all

Posted on 2015-01-27
10
68 Views
Last Modified: 2015-01-27
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

0
Comment
Question by:AndreasHermle
  • 5
  • 4
10 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40572446
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
 

Author Comment

by:AndreasHermle
ID: 40572455
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
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40572460
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
 

Author Comment

by:AndreasHermle
ID: 40572470
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
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40572471
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:AndreasHermle
ID: 40572477
Hi Philipp

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

Regards, Andreas
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40572479
Can you please post your document.
0
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 40572488
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
 

Author Comment

by:AndreasHermle
ID: 40572503
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
 

Author Closing Comment

by:AndreasHermle
ID: 40572504
Great job, as always :-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
The Selection object is designed for user interaction. It has a Range property, so it can be used in most places that a Range object can. Recorded macros must use the Selection because they are simply copying what the user is doing. A Range prope…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now