Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2017, 05:15 AM
cooloox cooloox is offline Auto Generation of Hyperlinks Windows 10 Auto Generation of Hyperlinks Office 2016
Novice
Auto Generation of Hyperlinks
 
Join Date: Mar 2017
Posts: 3
cooloox is on a distinguished road
Unhappy Auto Generation of Hyperlinks

Hi everyone,



I am new to these forums. I am just a VBA dabbler and I am stuck at trying to figure out a macro.

I want to make a manual table of contents on a Table of Contents worksheet with hyperlinks to different headings on different worksheets in the file. To make this easier than manually producing each hyperlink I am trying to write a macro to assist me. I want to be able to select a cell and have my macro add the text of that cell to the Table of Contents worksheet and turn it into a hyperlink pointing to the cell I selected.

My code so far is ALMOST there! The only issue I am having is being able to capture both the worksheet and cell reference when producing the hyperlink. My code is setting the correct cell reference, but not the correct worksheet. Here is my code:

Sub createHyperlinks()
'
' Macro to create hyperlinks
'
'
Dim tocText As String ' variable to hold text to appear in table of contents
Dim sh As Worksheet ' holds name of worksheet to be linked to
Dim selectedCell As Range ' holds cell reference of cell I want to be linked to

Set sh = ActiveSheet
Set selectedCell = ActiveCell
tocText = ActiveCell.Text
Sheets("Table of Contents").Select

ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=selectedCell.Address, TextToDisplay:=tocText

End Sub


Let's say cell A5 on Worksheet 2 has text in it = "Cover 1". I click on that cell and run the above code. "Cover 1" appears in the Table of Contents worksheet and is converted to a hyperlink and does point to cell A5, but it points to A5 on the "Table of Contents" worksheet, not A5 on the "Worksheet 2" worksheet.

Every possible way of referring to Worksheet 2 I could think of resulted in an error. Can anyone help me with this please?

Last edited by cooloox; 03-27-2017 at 05:20 AM. Reason: To make my post more understandable
Reply With Quote
  #2  
Old 03-27-2017, 06:12 AM
NoSparks NoSparks is offline Auto Generation of Hyperlinks Windows 7 64bit Auto Generation of Hyperlinks Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

I used the macro recorder to see how/where Excel uses the sheet name.
Give this a try...
Code:
Sub createHyperlinks_2()
'
' Macro to create hyperlinks
'
'
Dim tocText As String       ' variable to hold text to appear in table of contents
Dim sh As String            ' holds name of worksheet to be linked to
Dim selectedCell As Range   ' holds cell reference of cell I want to be linked to

sh = ActiveSheet.Name
Set selectedCell = ActiveCell
tocText = ActiveCell.Text
Sheets("Table of Contents").Select

ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", _
        SubAddress:=sh & "!" & selectedCell.Address, TextToDisplay:=tocText

End Sub
Reply With Quote
  #3  
Old 03-27-2017, 06:26 AM
cooloox cooloox is offline Auto Generation of Hyperlinks Windows 10 Auto Generation of Hyperlinks Office 2016
Novice
Auto Generation of Hyperlinks
 
Join Date: Mar 2017
Posts: 3
cooloox is on a distinguished road
Default

Hi NoSparks,

Wow!! Thank you very much!!! You were spot on and I must have pasted the code into Excel incorrectly the first time. I think it was to do with the wrapped line. The macro works flawlessly and does exactly what I need it to do. I am very grateful for your help as I would not have been able to work this out for myself. I have been messing with this for hours. I can't believe you figured it out so quickly.

Thanks again, your help was most appreciated, have a fantastic day!
Reply With Quote
  #4  
Old 03-27-2017, 06:37 AM
cooloox cooloox is offline Auto Generation of Hyperlinks Windows 10 Auto Generation of Hyperlinks Office 2016
Novice
Auto Generation of Hyperlinks
 
Join Date: Mar 2017
Posts: 3
cooloox is on a distinguished road
Default

Is there something I need to do to mark this issue as solved? The first response from NoSparks fixed the issue beautifully.
Reply With Quote
  #5  
Old 03-27-2017, 06:47 AM
NoSparks NoSparks is offline Auto Generation of Hyperlinks Windows 7 64bit Auto Generation of Hyperlinks Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

Glad I could help.

Supposedly there is something under the Thread Tools drop down, upper right on page, for marking the thread solved.
Reply With Quote
Reply

Tags
hyperlinks, toc



Similar Threads
Thread Thread Starter Forum Replies Last Post
Reports Filter Generation ebilbrough Project 3 01-05-2016 06:13 AM
Auto Generation of Hyperlinks Awkward dropdown list generation copwriter Excel 3 06-28-2014 05:59 PM
Auto Generation of Hyperlinks Invoice Number Generation mrphilk Excel 2 06-08-2010 12:39 PM
How to reduce top margin in even and odd pages in pdf generation using msword Nandu4u Word 0 11-20-2009 07:16 AM
Templates: automatic text generation from Rich Text content control Chickenmunga Word 0 10-01-2008 11:16 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:43 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft