View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
ct60 ct60 is offline
external usenet poster
 
Posts: 26
Default Using Excel Hyperlinks to refer to a Subroutine rather than a Cell

Let me answer my own question.

There is a Workbook Event "Workbook_SheetFollowHyperlink" which allows for
code to be run after a hyperlink is clicked. So, I put in the following code
which does what I was hoping for:

Private Sub Workbook_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target
As Hyperlink)
Dim rangeString As String
Dim rangeSubString As String
Dim whereExclaim As Integer
Dim scrollCol As Integer

rangeString = Target.SubAddress

whereExclaim = InStr(1, Target.SubAddress, "!")

If whereExclaim = 0 Then
Exit Sub
End If

rangeSubString = Mid(rangeString, whereExclaim + 1)

scrollCol = Range(rangeSubString).Column

ActiveWindow.ScrollColumn = scrollCol
End Sub

Note that, I also freeze the panes of each worksheet first then set the
scroll column. The net effect is that the data is lined up very nicely with
the frozen columns to the left and the headers at the top.

Nice - if I do say so myself.

Hope this helps.

Chris )

"ct60" wrote:

Hello Again -

I have found that given a list of worksheet names I can create hyperlinks to
those worksheets (let's say specifically Cell P2) using the following code:

ws.Activate

For row = tRow To bRow
ws.Cells(row, col).Select
With Selection
t2D = Selection.Value ' Note that the Cell value is the WS Name
subA = "'" & t2D & "'!P2"

.ClearContents
ws.Hyperlinks.Add Anchor:=Selection, Address:="",
SubAddress:=subA, TextToDisplay:=t2D
End With
Next

I can also scroll a worksheet to exactly the right cell using:

Sub scroll2Column(wsName)
Sheets(wsName).Activate
ActiveWindow.ScrollColumn = 16
ActiveSheet.Range("P2").Select
End Sub

I would like to combine these two concpets and create a hyperlink which
doesn't just refer to the cell but to the subroutine which brings the user to
the cell and then conviently scrolls the worksheet as well.

Despite many attempts at this, I have not gotten that to work.

Does anyone know how this might be done?

Thanks in advance,

Chris Tauss )