High Plains Grifter
Board Regular
- Joined
- Mar 9, 2010
- Messages
- 129
Hello, people.
I have the following macro, which is assigned to ctrl+shift+h to allow users to make a list and to insert hyperlinks to other sheets that have more details than the list can show:
I was surprised to see that even if the contents of the active cell is not a valid sheet name in the target document, the link is still inserted without error. If the link is subsequently clicked, the target document will open and an "invalid reference" error box will appear.
Is there any way to check whether the contents of the cell constitutes a valid sheet name in the target workbook at the point at which the hyperlink is created, without opening up the target document?
Thanks for your time!
I have the following macro, which is assigned to ctrl+shift+h to allow users to make a list and to insert hyperlinks to other sheets that have more details than the list can show:
Code:
Sub linker()
Dim strDocument As String
Dim strSheet As String
Dim strAddress As String
Dim strLink As String
'assume control of the status bar and make sure we have a clean slate
Application.DisplayStatusBar = True
Application.StatusBar = ""
'The department, which is used to decide which document to link to, is in coloumn C
strLink = ActiveSheet.Range("C" & ActiveCell.Row).Value
'If the user has not selected a workflow to base the link upon, give them a snotty message
If ActiveCell.Column <> 12 Then
Application.StatusBar = "Automatic link function is only valid for workflows - please select a valid workflow name"
Else
'Carry on
End If
'If the department is assistance, we want to go to the assistance control sheet
If strLink = "Assistance" Then
strDocument = "List 08.1 Assistance Workflows and Tasks.xlsm"
'If the department is claims, go there
ElseIf strLink = "Claims" Then
strDocument = "List 08.2 Claims Workflows and Tasks.xlsm"
'if the department is neither claims nor assistance then hit the user about the head
Else
MsgBox "If you wish to add a hyperlink, you need to list whether the workflow is Claims or Assistance", vbOKOnly, "Cannot Find Link Document"
Exit Sub
End If
'The target documents are all in the lists folder, so stick that in
strAddress = "\\Cg4\shared\Projects\GoTrex\Lists\"
'The name of the workflow (in the highlighted cell) should also be the name of a sheet in the target document,
'if the workflow has been properly documented, so get that
strSheet = ActiveCell.Value
'if the user is an idiot, kick them out
If strSheet = "" Then GoTo errs
ActiveCell.Select
'If it still effs up then I guess we should apologise or something
On Error GoTo errs
'Stick a hyperlink in the cell by joining up the address, document and sheet name we have taken.
ActiveSheet.Hyperlinks.Add Anchor:=Selection, _
Address:=strAddress & strDocument, _
SubAddress:="'" & strSheet & "'!A1", TextToDisplay:=strSheet
'Move down one cell, so the user can clearly see that everything has gone according to plan
ActiveCell.Offset(1, 0).Activate
'relinquish control of the status bar
Application.DisplayStatusBar = False
Exit Sub
errs:
Application.StatusBar = "No page has been found in '" & strDocument & "' to match the workflow title you have listed."
End Sub
I was surprised to see that even if the contents of the active cell is not a valid sheet name in the target document, the link is still inserted without error. If the link is subsequently clicked, the target document will open and an "invalid reference" error box will appear.
Is there any way to check whether the contents of the cell constitutes a valid sheet name in the target workbook at the point at which the hyperlink is created, without opening up the target document?
Thanks for your time!