?
Solved

Run a macro code only on certain tables not all

Posted on 2015-01-27
10
Medium Priority
?
75 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 

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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Suggested Courses

752 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