Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

How to Make Sheet HyperLink go to 2nd Screen

0

Hello,

I have a table of contents in my workbook that contains hyperlinks to each sheet. I have two windows open.  Is there a way that when selecting the hyperlink  that  sheet opens in the second window? 

Answer
Discuss

Answers

0
Selected Answer

Hi again Susan and Happy New Year!

I can only think to do this in VBA and there's probably a better way than this one...

In the first attached file, the first sheet "Table of Contents" (aka TOC) has a single hyperlink in cell A2  which points to cell A5 in the second sheet "Introduction".

If you open a second window (showing TOC) then return to the first window and click A2, this event macro is triggered (with comments for your information)

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    Dim HypWs As String, HypAddr As String

    ' see if there's a second window...
    If ThisWorkbook.Windows.Count > 1 Then
        ' extract worksheet name and cell from hyperlink
        HypWs = Left(Target.SubAddress, InStr(Target.SubAddress, "!") - 1)
        HypAddr = Mid(Target.SubAddress, InStr(Target.SubAddress, "!") + 1, 12)

        Application.EnableEvents = False
        ' go back to TOC on sheet1
         Sheets(1).Activate
         ' move to second window...
         Windows(2).Activate
         ' ... then show the hyperlink's sheet and move to the target address
         With Worksheets(HypWs)
            .Activate
            .Range(HypAddr).Select
         End With
    End If

    Application.EnableEvents = True

End Sube

Window 1 will show TOC and window 2 will be activated with cell A5 selected. 

It works because the "within file" hyperlink (Target.Subaddress) takes the form:

Introduction!A5

You can use this with any other hyperlink on Sheet1 but they need to be that simple form.

Revision 05 January 2024

This responds to your file attached to the Question and your Discussion comment "I noticed that when in Excel, editing the hyperlink and viewing the Cell Reference, some of the sheet names are in quotes, some are not.  Those sheets in quotes will not work with the hyperlink vba code (See hyperlink to "Basic-Dyes").

Not sure why that happens but in the SECOND file attached, I've used your file but simply added a line to remove the quoptation marks when the macro extracts the sheet name from the hyperlink (changes in bold in the code extract below):

        ' extract worksheet name and cell from hyperlink
        HypWs = Left(Target.SubAddress, InStr(Target.SubAddress, "!") - 1)
        ' remove any quote marks from sheet name
        HypWs = Replace(HypWs, "'", "")

This should work with all your hyperlinks (though I had to Edit the hyperlink in cell A9 "AY23" to refer to the worksheet). Also I removed the =HYPERLINK formula for Freight and replaced it with a hyperlink link like the rest.. Accordingly I've removed your Module1 (not needed). I also renamed your tab "Sheet1" to "Contents" for better reading (it's still theleftmost sheet so has the code index 1 in Sheets(1) BTW).

Note that to reset the "read" hyperlinks, I selected all the cells. copied them to another column then cut them and pasted them back in column A.

Revision 06 January 2024

In the THIRD attached file, the event procedure above has been modified so

  1. if there's just one window, the code opens a new one
  2. the second window is moved to the right so column A of the table of contents (TOC) is visible
  3. the hyperlinked page is shown on the right (even if the hyperlink was launched form the rightmost window).

Changes are in bold below:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

    Dim HypWs As String, HypAddr As String

    ' see if there's a second window...
    If ThisWorkbook.Windows.Count = 1 Then

        ' if only one, create a new one and pisition to the right
        ThisWorkbook.NewWindow
        Windows(1).Left = Windows(1).Left + Sheet1.Columns(1).Width + 15
        ' return left sheet to TOC
        Windows(2).Activate
        Sheets(1).Activate
        ' make rightmost window active
        Windows(2).Activate

        Else
        ' extract worksheet name and cell from hyperlink
        HypWs = Left(Target.SubAddress, InStr(Target.SubAddress, "!") - 1)
        ' remove any quote marks from sheet name
        HypWs = Replace(HypWs, "'", "")
        HypAddr = Mid(Target.SubAddress, InStr(Target.SubAddress, "!") + 1, 30)

        Application.EnableEvents = False
        ' go back to TOC on sheet1
         Sheets(1).Activate
         ' move to second window...
         Windows(2).Activate
         ' move it so TOC column A is visible
         Windows(1).Left = Windows(2).Left + Sheet1.Columns(1).Width + 15
         ' ... then show the hyperlink's sheet and move to the target address
         With Worksheets(HypWs)
            .Activate
            .Range(HypAddr).Select
         End With
    End If

    Application.EnableEvents = True


End Sub

Note that the active window always has index number 1 so is  .Windows(1)

Hope this works for you, If so, please remember to mark this Answer as Selected.

Discuss

Discussion

Susan

My bad- for speed I cut out the bits which made  the code workbook-specific.

Please edit your question to add your file and I'll look at the code names of the sheets and correct tomorrow hopefully (it's getting late here). You should probably remove the contents of the hyperlinked pages.

Also, did you try my file alone to prove the concept?
John_Ru (rep: 6142) Jan 3, '24 at 4:55 pm
John_Ru, Thank you for the quick response.  I copied the code and have it partially working!

There is something funky going on with the hyperlink format, I will get back with you in the morning.
SusanUser (rep: 16) Jan 3, '24 at 6:28 pm
Okay Susan. If you struggle, please attach a file with your hyperlinks and sheets (but no data) and I will try to sort it and append to my Answer. 

I'm assuming you got my demo file to work, right? (This one, not Turkey Manager!) 
John_Ru (rep: 6142) Jan 4, '24 at 5:31 am
Yes, thank you for the Turkey Manager, that worked! 

Yes, I recieved your demo file and it worked.

I don't know how to attach a file to a response.  I tried to create a new post and attach a file but got an error saying something to the effect that I've posted too many times in 24 hours.  Perhaps you could help with that.

In order to figure out what was going on (instead of having a Private Sub) I created  a routine to pass a hyperlink from my sheet to the code you created. 

The name of the routine that passes the hyperlink is "GetHlink".  This code works in some of my hyperlinks  column A, Sheet1.  For example, when the hyperlink in cell A12 or A20 (opening the "Pigments" or "Tunnel" sheets respectively) is passed to your code, it works.  However, the other hyperlinks do not work and give error, for example using the hyperlink in cell A11.

I noticed that when in excel, editing the hyperlink and viewing the Cell Reference, some of the sheet names are in quotes, some are not.  Those sheets in quotes will not work with the hyperlink vba code (See hyperlink to "Basic-Dyes").  Those without do work ("Pigments" and "Tunnel").  I can't figure out how to remove the quotes.

  I slightly modified your code as follows to a new routine called "ClickLink"
1.  I passed my hyperlink.
2.  I changed the length of the hyperlink sheet name to allow for 30 characters

Thanks ahead of time for your help.
SusanUser (rep: 16) Jan 4, '24 at 11:28 am
Susan

You can't attach an Excel file to a response but you can attach up to 3 files to your original Question (go to Edit then the Add Files... button). That will really help since I have little time left today.

Not sure where the quotation marks in hyperlinks came from but let me see. If I can't fix it, I can easily add a line of code to test and get around that.

The 24-hours limit on posts applies to new(ish) users and was introduced to stop a big spam attack some time ago. I can't do anything about it (it's not my site) but that limit gets lifted when you have a certain Reputation (think it's 15 points).
John_Ru (rep: 6142) Jan 4, '24 at 12:15 pm
File attached.
SusanUser (rep: 16) Jan 4, '24 at 12:54 pm
Thanks Susan. Will look at it tomorrow (a slight family emergency cropped up for me) 
John_Ru (rep: 6142) Jan 4, '24 at 4:06 pm
Susan. Please see my "Revision 05 January 2024" to my Answer and your file (revised and working well I think!).

Incidentally, it looks like your file contains real data (tab Anionic-Dyes and those to its right) . If you need that removing, please replace your file in the Question with a data free one and I'll modify my second file to reflect that.
John_Ru (rep: 6142) Jan 5, '24 at 3:20 am
I'm busy with another project today, I did want to let you know that I briefly looked at your latest revision.  On initial look, when I click the links the referenced sheet opens on the 1st window.  I'll take a closer look on Monday.  
SusanUser (rep: 16) Jan 5, '24 at 2:54 pm
Susan, when you look, be sure to have the Contents tab in the window where the title ends "-1" not "-2".

I suppose you could detect the window the hyperlink was launched from (and display it in the other window) but I think that's straying quite a bit from your initial question, which I answered conceptually.
John_Ru (rep: 6142) Jan 5, '24 at 3:01 pm
Susan, please see Revision 05 January 2024 to my Answer and the third file. It implements the suggestion I made above. Hope this makes sense and works well for you when you test on Monday.
John_Ru (rep: 6142) Jan 6, '24 at 8:41 am
Beautiful!  Wow!  Thank you thank you!  Works like a charm!

I am going to do some research on how Private Sub works and how worksheet events work.  I now know this can be very powerful and am very excited to implement it.  Until now I didn't know this feature existed! 

Thank you thank you thank you.
SusanUser (rep: 16) Jan 8, '24 at 10:05 am
Glad that worked for you Susan. Thanks again for selecting my Answer.
John_Ru (rep: 6142) Jan 8, '24 at 12:04 pm
You Rock!
SusanUser (rep: 16) Jan 8, '24 at 4:25 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login