Consulting

Results 1 to 9 of 9

Thread: Hyperlink query

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location

    Hyperlink query

    In the attached file, the search (enter search term in cell B1 on FindWord)creates hyperlinks to the found cells, the hyperlinks fail, however, if there is a bracket in the sheet name. Is there a way around this?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  2. #2
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Don't you need to surround the sheet with single quotation marks when it has spaces? (Maybe that's a stupid question.)

    PS. I changed the third sheet's name to BoQ2 and the links work with that.

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    In the attached file, the search (enter search term in cell B1 on FindWord)creates hyperlinks to the found cells, the hyperlinks fail, however, if there is a bracket in the sheet name. Is there a way around this?
    It's the space in the sheet name MD. Tryt this alternate


    Option Compare Text
    Option Explicit
    
    Public Sub FindAll(Search As String)
    Dim WB              As Workbook
        Dim WS              As Worksheet
        Dim Cell            As Range
        Dim Prompt          As String
        Dim Title           As String
        Dim FindCell()      As String
        Dim FindSheet()     As String
        Dim FindLink()      As String
        Dim FindText()      As String
        Dim Counter         As Long
        Dim FirstAddress    As String
        Dim MyResponse      As VbMsgBoxResult
        Dim Exists          As Boolean
        Dim k               As Long
    'Cancel events
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    'Loop through sheets; add location and text to array
        On Error Resume Next
        Set WB = ActiveWorkbook
        If Err = 0 Then
            On Error GoTo 0
            For Each WS In WB.Worksheets
                If WS.Name <> "FindWord" Then
                    With WB.Sheets(WS.Name).Range("D:D")
                        Set Cell = .Find(What:=Search, LookIn:=xlValues, LookAt:=xlPart, _
                            MatchCase:=False, SearchOrder:=xlByColumns)
                        If Not Cell Is Nothing Then
                            FirstAddress = Cell.Address
                            Do
                                Counter = Counter + 1
                                ReDim Preserve FindCell(1 To Counter)
                                ReDim Preserve FindSheet(1 To Counter)
                                ReDim Preserve FindText(1 To Counter)
                                ReDim Preserve FindLink(1 To Counter)
                                FindCell(Counter) = Cell.Address(False, False)
                                FindText(Counter) = Cell.Text
                                FindSheet(Counter) = WS.Name
                                If InStr(1, WS.Name, " ") > 0 Then
                                    FindLink(Counter) = "'" & WS.Name & "'"
                                Else
                                    FindLink(Counter) = WS.Name
                                End If
                                Set Cell = .FindNext(Cell)
                            Loop While Not Cell Is Nothing And Cell.Address <> FirstAddress
                        End If
                    End With
                End If
            Next
        End If
        On Error GoTo 0
    'Exit if nothing found
        If Counter = 0 Then
            MsgBox Search & " was not found.", vbInformation, "Zero Results For Search"
            GoTo Cancelled
        End If
    'Write found data to sheet
        On Error Resume Next
        Sheets("FindWord").Select
        Range("A3:H65536").ClearContents
        For Counter = 1 To UBound(FindCell)
            ActiveSheet.Hyperlinks.Add Anchor:=Range("A" & Counter + 2), _
                Address:="", SubAddress:=FindLink(Counter) & "!" & FindCell(Counter), _
                TextToDisplay:=FindSheet(Counter) & "!" & FindCell(Counter)
                Range("B" & Counter + 2).Value = FindText(Counter)
            'Add data from rows
            For k = 3 To 8
                Cells(Counter + 2, k).Formula = Sheets(FindSheet(Counter)) _
                        .Range(FindCell(Counter)).Offset(0, k - 2).Value
            Next k
        Next Counter
    'Clear variables and reset events
    Cancelled:
        Set WB = Nothing
        Set WS = Nothing
        Set Cell = Nothing
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        Application.EnableEvents = True
    End Sub

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ACtually, less changes


    Option Compare Text
    Option Explicit
    
    Public Sub FindAll(Search As String)
    Dim WB              As Workbook
        Dim WS              As Worksheet
        Dim Cell            As Range
        Dim Prompt          As String
        Dim Title           As String
        Dim FindCell()      As String
        Dim FindSheet()     As String
        Dim FindText()      As String
        Dim Counter         As Long
        Dim FirstAddress    As String
        Dim MyResponse      As VbMsgBoxResult
        Dim Exists          As Boolean
        Dim k               As Long
    'Cancel events
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
    'Loop through sheets; add location and text to array
        On Error Resume Next
        Set WB = ActiveWorkbook
        If Err = 0 Then
            On Error GoTo 0
            For Each WS In WB.Worksheets
                If WS.Name <> "FindWord" Then
                    With WB.Sheets(WS.Name).Range("D:D")
                        Set Cell = .Find(What:=Search, LookIn:=xlValues, LookAt:=xlPart, _
                            MatchCase:=False, SearchOrder:=xlByColumns)
                        If Not Cell Is Nothing Then
                            FirstAddress = Cell.Address
                            Do
                                Counter = Counter + 1
                                ReDim Preserve FindCell(1 To Counter)
                                ReDim Preserve FindSheet(1 To Counter)
                                ReDim Preserve FindText(1 To Counter)
                                FindCell(Counter) = Cell.Address(False, False)
                                FindText(Counter) = Cell.Text
                                FindSheet(Counter) = WS.Name
                                Set Cell = .FindNext(Cell)
                            Loop While Not Cell Is Nothing And Cell.Address <> FirstAddress
                        End If
                    End With
                End If
            Next
        End If
        On Error GoTo 0
    'Exit if nothing found
        If Counter = 0 Then
            MsgBox Search & " was not found.", vbInformation, "Zero Results For Search"
            GoTo Cancelled
        End If
    'Write found data to sheet
        On Error Resume Next
        Sheets("FindWord").Select
        Range("A3:H65536").ClearContents
        For Counter = 1 To UBound(FindCell)
            ActiveSheet.Hyperlinks.Add Anchor:=Range("A" & Counter + 2), _
                                       Address:="", _
                                       SubAddress:="'" & FindSheet(Counter) & "'!" & FindCell(Counter), _
                                       TextToDisplay:=FindSheet(Counter) & "!" & FindCell(Counter)
            Range("B" & Counter + 2).Value = FindText(Counter)
            'Add data from rows
            For k = 3 To 8
                Cells(Counter + 2, k).Formula = Sheets(FindSheet(Counter)) _
                        .Range(FindCell(Counter)).Offset(0, k - 2).Value
            Next k
        Next Counter
    'Clear variables and reset events
    Cancelled:
        Set WB = Nothing
        Set WS = Nothing
        Set Cell = Nothing
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        Application.EnableEvents = True
    End Sub

  5. #5
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Maybe my suggestion...err...question...wasn't so stupid?

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by xCav8r
    Maybe my suggestion...err...question...wasn't so stupid?
    No, spot-on!

    I had already posted before I saw it.

  7. #7
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    xld, I tried your code already..
    sad to say I cannot get it work....

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by sheeeng
    xld, I tried your code already..
    sad to say I cannot get it work....
    You need MD's worrkbook event code as well to fire it.

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks all.
    Sometimes you're just "too close" to see the simple problem!
    Regards
    Malcolm
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •