Link to home
Create AccountLog in
Avatar of Andreas Hermle
Andreas HermleFlag 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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of 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
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

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
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Hi Graham,

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

Thank you very much for your professional help.

Regards, Andreas
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
uups I made the same mistake again. I inadvertently graded my own comment.
Hi Graham, great job, thank you very much for your professional and swift help.

Regards, Andreas
HeHe
Thanks Andreas