'
' The purpose of this macro is to move directly towards a defined bookmark comment within the first cell
' of a line. Each first cell will be checked whether it starts with "====== Bookmark Info ======"
' The user is asked for a bookmark name and the procedure will go through the firxt column and tries to
' find a matching bookmark. It it exists it will move to the same column as the current cell but to the
' row of the requested bookmark.
' If the bookmark doesn't exist the user will be notified about it.
'
' Macro created 14-06-2007 by Bob Becker Hof (HP)
' Tested with Excel 2003 and EVE 2.30 xls output format
'

Sub Comment_Bookmark_lines_GoTo()

Dim crow As Long
Dim ccol As Long
Dim lasrow As Long
Dim lascol As Long
Dim trow As Long
Dim newrow As Boolean
Dim csname As String
Dim prgname As String

Dim msg As String
Dim Style As Long
Dim Title As String
Dim Response As Long

Dim MultiRange As Range
Dim hidden_rows_behind_lasrow As Long
    
Dim currentcomment As String
Dim currentcommentmaxlen As Integer
Dim commenttextstart As String
Dim commenttextend As String
Dim cmttext As String
Dim commentcolumn As Integer
Dim rng As Range
Dim commentendpos As Integer
Dim Markline As Boolean                             ' Boolean used for flagging we found a match or not

    Title = "Comment_Bookmark_lines_GoTo"          ' Define title.
    prgname = "%" + Title + ": "

    crow = ActiveCell.Row
    ccol = ActiveCell.Column
    lasrow = ActiveCell.SpecialCells(xlLastCell).Row
    lascol = ActiveCell.SpecialCells(xlLastCell).Column

    commenttextstart = "====== Bookmark Info ======"
    commenttextend = commenttextstart
    commentcolumn = 1
    Markline = False
    currentcommentmaxlen = 255

'
' By default the xlLastCell.row funtion returns the last visible row
' But there might be more rows below it which are hidden so loop down until we hit a row which is NOT hidden
' Lets assume that 650000 is the maximum row number possible...
'
''    Debug.Print "Current Lasrow: " & Str(lasrow)
    For hidden_rows_behind_lasrow = lasrow To 650000 Step 1
        If Not (Range(Cells(hidden_rows_behind_lasrow + 1, 1).Address).EntireRow.Hidden) Then
            lasrow = hidden_rows_behind_lasrow
''            Debug.Print "New Lasrow    : " & Str(lasrow)
            Exit For
        End If
    Next

    csname = ActiveSheet.Name

    If Range(Cells(crow, commentcolumn).Address).EntireColumn.Hidden Then
        msg = "Do you want to unhide the first column for displaying the bookmarks?"    ' Define message.
        Style = vbYesNo + vbWarning + vbDefaultButton1     ' Define buttons.
        Response = MsgBox(msg, Style, Title)
        If Response = vbYes Then                            ' User chose Yes.
            Columns(commentcolumn).EntireColumn.Hidden = False
        End If
    End If

'
' Ask for unique marker name to be located
'

    cmttextname = InputBox("Enter unique bookmark name without equal (=) sign:", Title)
    If cmttextname = "" Then
        GoTo Finish
    End If
    
'
' First define the unique starter for the bookmark
'
define_cmttextkey:
    cmttextname = UCase(cmttextname)
    cmttextkey = commenttextstart & Chr(10) & cmttextname & " = "
    
' Temporarily stop screen updates to speed up the process
    Application.ScreenUpdating = False


'
' Check whether the marker name entered exists anywhere
'
    For trow = lasrow To 1 Step -1
        Set rng = ActiveSheet.Cells(trow, commentcolumn)
        If Not (rng.Comment Is Nothing) Then
            currentcomment = rng.Comment.Text
''            Debug.Print prgname & "Cell at row " & crow & " column 1 has comment/bookmark set to " & Chr(10) & currentcomment & Chr(10) & "within worksheet " & csname
            If Left(currentcomment, Len(cmttextkey)) = cmttextkey Then
                Markline = True
                Exit For
            End If
        End If
    Next

' Enable screen updates again
    Application.ScreenUpdating = True
    
    If Markline Then
        If (Range(Cells(trow, commentcolumn).Address).EntireRow.Hidden) Then
            If Len(currentcomment) > currentcommentmaxlen Then
                currentcomment = Left(currentcomment, currentcommentmaxlen - 3) & "..."
            End If
            msg = "Line " & Trim(Str(trow)) & " is hidden with bookmark:" & Chr(10) & currentcomment & Chr(10) & Chr(10) & "Do you want to unhide the line?"   ' Define message.
            Style = vbYesNo + vbWarning + vbDefaultButton1     ' Define buttons.
            Response = MsgBox(msg, Style, Title)
            If Response = vbYes Then                            ' User chose Yes.
                Range(Cells(trow, commentcolumn).Address).EntireRow.Hidden = False
            Else
                Cells(crow, ccol).Select
                GoTo Finish
            End If
        End If
        Cells(trow, ccol).Select
    Else
        Cells(crow, ccol).Select
        msg = MsgBox(prgname + "Bookmark " + cmttextname + " not found within worksheet " + csname, vbInformation)
    End If

Finish:

End Sub