Avatar of Andreas Hermle
Andreas Hermle
Flag for Germany asked on

bookmark certain cells in current table using VBA

Dear Experts:

for the current table I would like to run the following action:

... bookmark the first and fourth cell of each row starting from ROW number 2
... the bookmarks are to be named as follows: bookmark_1, bookmark_2, bookmark_3, bookmark_4 etc. (sequential numbering)

That is: bookmark_1 (first cell of row 2)
bookmark_2 (fourth cell of row 2)
bookmark_3 (first cell of row 3)
bookmark_4 (fourth cell of row 3)
bookmark_5 (first cell of row 4)
bookmark_6 (fourth cell of row 4)
etc.

Help is much appreciated.

Thank you very much in advance.

Regards, Andreas
Microsoft Word

Avatar of undefined
Last Comment
GrahamSkan

8/22/2022 - Mon
GrahamSkan

Hi Andreas, the secret to learning to code is to try it.
Sub BookMarkCells()
    Dim bmk As Bookmark
    Dim tbl As Table
    Dim rw As Row
    Dim b As Integer
    
    Set tbl = Selection.Tables(1)
    For Each rw In tbl.Rows
        b = b + 1
        ActiveDocument.Bookmarks.Add "Bookmark_" & b, rw.Cells(1).Range
        b = b + 1
        ActiveDocument.Bookmarks.Add "Bookmark_" & b, rw.Cells(4).Range
    Next rw
End Sub

Open in new window

Andreas Hermle

ASKER
I got the message, Graham, I have understood ;-)

I swear to heed your advice starting from tomorrow :-)

Works great but as a matter of fact the macro is to start only from row 2 of the current table.

Regards, Andreas
GrahamSkan

I believe you, Andreas.

This version avoids setting the bookmarks in the first row
Sub BookMarkCells2()
    Dim bmk As Bookmark
    Dim tbl As Table
    Dim rw As Row
    Dim b As Integer
    
    Set tbl = Selection.Tables(1)
    For Each rw In tbl.Rows
        If rw.Cells(1).RowIndex > 1 Then
            b = b + 1
            ActiveDocument.Bookmarks.Add "Bookmark_" & b, rw.Cells(1).Range
            b = b + 1
            ActiveDocument.Bookmarks.Add "Bookmark_" & b, rw.Cells(4).Range
        End If
    Next rw
End Sub

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Andreas Hermle

ASKER
Works great, Graham, thank you very much. But I am afraid there is still one thing that needs to be tweaked. I failed to mention this requirement in the beginning.

The bookmarks now also include the cell end markers, but I would like to only the text in the cell bookmarked excluding this cell end marker.

Thank you very much in advance.

Regards, Andreas
ASKER CERTIFIED SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
Hi Graham,

as always works like a charm, exactly as I wanted it.

Thank you very much for your professional help.

Regards, Andreas
Andreas Hermle

ASKER
I've requested that this question be closed as follows:

Accepted answer: 0 points for AndreasHermle's comment #a40049717

for the following reason:

Great job, thank you very much, Regards, Andreas
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Andreas Hermle

ASKER
uups I made the same mistake again. I inadvertently graded my own comment.
Andreas Hermle

ASKER
Hi Graham, great job, thank you very much for your professional and swift help.

Regards, Andreas
GrahamSkan

HeHe
Thanks Andreas
Your help has saved me hundreds of hours of internet surfing.
fblack61