We help IT Professionals succeed at work.

bookmark certain cells in current table using VBA

Andreas Hermle
on
1,141 Views
Last Modified: 2014-05-08
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
Comment
Watch Question

GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
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 HermleTeam leader

Author

Commented:
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
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
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

Andreas HermleTeam leader

Author

Commented:
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
Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Andreas HermleTeam leader

Author

Commented:
Hi Graham,

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

Thank you very much for your professional help.

Regards, Andreas
Andreas HermleTeam leader

Author

Commented:
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
Andreas HermleTeam leader

Author

Commented:
uups I made the same mistake again. I inadvertently graded my own comment.
Andreas HermleTeam leader

Author

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

Regards, Andreas
GrahamSkanRetired
CERTIFIED EXPERT
Top Expert 2012

Commented:
HeHe
Thanks Andreas

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.