'
' The purpose of this macro is to move directly towards the next available defined bookmark comment
' below the current line. If we hit the last line we ask whether we should continue from the top or not.
' Each first cell, from the current line+1, will be checked whether it starts with "====== Bookmark Info ======"
' If the bookmark doesn't exist the user will be notified about it and we will stay where we are.
'
' 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_Next_Match()

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_Next_Match"          ' 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

' Temporarily stop screen updates to speed up the process
    Application.ScreenUpdating = False

    For trow = crow + 1 To lasrow 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(commenttextstart)) = commenttextstart Then
                Markline = True
                Exit For
            End If
        End If
    Next

'
' In case we did NOT find the string we where looking for start at the top downwords until we hit the current row
' But  we do this only if the user agrees ;-)
'
    If Not (Markline) Then
        msg = "Continue search from other end of file?"         ' Define message.
        Style = vbYesNo + vbInformational + vbDefaultButton1    ' Define buttons.
        Response = MsgBox(msg, Style, Title)
        If Response = vbYes Then                                ' User chose Yes.
            For trow = 1 To crow - 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(commenttextstart)) = commenttextstart Then
                        Markline = True
                        Exit For
                    End If
                End If
            Next
        End If
    End If

' 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 + "No next bookmark found within worksheet " + csname, vbInformation)
    End If

Finish:

End Sub