+ Reply to Thread
Results 1 to 5 of 5

Search multiple worksheets for matching cell value and return cell location as a hyperlink

Hybrid View

  1. #1
    Registered User
    Join Date
    08-16-2012
    Location
    Indiana
    MS-Off Ver
    Excel 2013
    Posts
    3

    Search multiple worksheets for matching cell value and return cell location as a hyperlink

    I've been searching for a way to do this, and I apologize if a duplicate solved thread is already on here. I have run out of brain cells on search keywords.

    I have a 9-worksheet workbook. On a "master" worksheet, column A lists out course names for a curriculum. Column B lists the corresponding week in which that course exists. Each other worksheet provides a week in the curriculum with corresponding courses, times, relevant information, etc.

    What I want is for column C to provide a hyperlink to the cell address in the appropriate worksheet, based on the course name in column A.

    Example:
    A-Name B-Week C-Link
    Intro to Engineering 2 "Link to week 2, Intro to Engineering (cell C4)"

    Thank you in advance!
    Laura

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Search multiple worksheets for matching cell value and return cell location as a hyper

    Hello and welcome to the forum,

    please check the attached example which is checking the name and week in columns 1 and 2 in all worksheets to find the address for a hyperlink: Hyperlink.xlsm
    Note, I assumed that each entry in the master sheet can be found only ONCE in another sheet.

    Contains:
    Option Explicit
    
    Sub GetLinks()
        Dim xlWs As Worksheet
        Dim xlRng As Range
        Dim strAddr$
        Dim i&
        
        On Error GoTo GetLinks_ErrorHandler
        Application.ScreenUpdating = False
    
        With ActiveSheet
            For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
                For Each xlWs In ThisWorkbook.Worksheets
                    If xlWs.Name <> .Name Then
                        Set xlRng = xlWs.Columns(1).Find(WHat:=.Cells(i, 1).Value, LookIn:=xlValues, lookat:=xlWhole)
                        If Not xlRng Is Nothing Then
                            strAddr = xlRng.Address
                            Do
                                If .Cells(i, 2).Value = xlRng.Offset(, 1).Value Then
                                    .Hyperlinks.Add Anchor:=.Cells(i, 3), Address:="", _
                                        SubAddress:="'" & xlWs.Name & "'!" & xlRng.Offset(, 2).Address(False, False), _
                                        TextToDisplay:="#'" & xlWs.Name & "!" & xlRng.Offset(, 2).Address(False, False)
                                    GoTo NextLine 'assuming its unique in the workbook
                                End If
                                Set xlRng = xlWs.Columns(1).FindNext(xlRng)
                                If xlRng Is Nothing Then Exit Do
                                If strAddr = xlRng.Address Then Exit Do
                            Loop
                        End If
                    End If
                Next xlWs
    NextLine:
            Next i
        End With
    
    GetLinks_Proc_Exit:
        Application.ScreenUpdating = True
        Exit Sub
    GetLinks_ErrorHandler:
        MsgBox "Error: " & Err.Number & " (" & Err.Description & ") in Sub 'GetLinks' of Module 'Module1'.", vbOKOnly + vbCritical, "Error"
        Resume GetLinks_Proc_Exit
    End Sub
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    08-16-2012
    Location
    Indiana
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Search multiple worksheets for matching cell value and return cell location as a hyper

    Thank you for the reply! It works wonderfully in your example. When I run the code in my workbook, I don't get anything. I suspect it's because I'm terrible at explaining. I don't want to search the Week # in column B. I just want to search the course name in column A, over the full workbook.

    In the other worksheets, the course names are not in column A, they are in column C, and their setup is different.
    A: other info
    B: other info
    C: Name of course
    D-L: other info


    Your assumption is correct, each course name entry is unique and none are repeated.
    Ex:
    Intro to Engineering
    Basic Math
    Intro to Statistics
    Calculus
    Linear Algebra


    Sorry about this. I'm a newbie at VB

  4. #4
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Search multiple worksheets for matching cell value and return cell location as a hyper

    Hi,

    the following is not checking the week number any more and looking for the course name in column C of the sheets:
    Sub GetLinks()
        Dim xlWs As Worksheet
        Dim xlRng As Range
        Dim strAddr$
        Dim i&
        
        On Error GoTo GetLinks_ErrorHandler
        Application.ScreenUpdating = False
    
        With ActiveSheet
            For i = 2 To .Cells(.Rows.Count, 1).End(xlUp).Row
                For Each xlWs In ThisWorkbook.Worksheets
                    If xlWs.Name <> .Name Then
                        Set xlRng = xlWs.Columns(3).Find(WHat:=.Cells(i, 1).Value, LookIn:=xlValues, lookat:=xlWhole)
                        If Not xlRng Is Nothing Then
                            .Hyperlinks.Add Anchor:=.Cells(i, 3), Address:="", _
                                SubAddress:="'" & xlWs.Name & "'!" & xlRng.Address(False, False), _
                                TextToDisplay:="#'" & xlWs.Name & "!" & xlRng.Address(False, False)
                        End If
                    End If
                Next xlWs
            Next i
        End With
    
    GetLinks_Proc_Exit:
        On Error GoTo 0
        Set xlWs = Nothing
        Set xlRng = Nothing
        Application.ScreenUpdating = True
        Exit Sub
    GetLinks_ErrorHandler:
        MsgBox "Error: " & Err.Number & " (" & Err.Description & ") in Sub 'GetLinks' of Module 'Module1'.", vbOKOnly + vbCritical, "Error"
        Resume GetLinks_Proc_Exit
    End Sub

  5. #5
    Registered User
    Join Date
    08-16-2012
    Location
    Indiana
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Search multiple worksheets for matching cell value and return cell location as a hyper

    Hmm. Now the code isn't doing anything in your example or my workbook. It runs, but nothing populates.

    Edit Disregard- it is working! I hadn't pulled over the macro into my workbook. Now I have to troubleshoot and see why it's not working for all rows, but it is for some. I'll report back.
    And a +1 rep for you!!

    Edit 2 And troubleshooting is finished. I think I had edited the course name on the master sheet, but not the corresponding sheet.


    THANK YOU again, tehNexus!
    Last edited by wickedsparrow; 07-05-2013 at 08:59 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1