I built a function :
Function GenerateHyperlink(NameFile as string)
MyStr = "C:\Test\2. Test\" & NameFile
With ActiveSheet
.Hyperlinks.Add anchor:=Range(ThisWorkbook.FullName & "#'" & ActiveCell.Parent.Name & "'!" & ActiveCell.Address), Address:=MyStr, TextToDisplay:="Open File"
End With
GenerateHyperlink = MyStr
End Function
This when I type in cell :
=GenerateHyperlink(A2)
It does put in cell : #VALUE!
and When I type in immediate window :
? GenerateHyperLink("Test.pdf")
It does display the hyperlink in cell.
What am I missing ?
Edit after comments I did the following
Function GenerateHyperlink(NameFile as string) as string
MyStr = "C:\Test\2. Test\" & NameFile
ActiveCell.Parent.Evaluate "DefineHy(" & ActiveCell.Address & "," & MyStr & "," & "This is a test" & ")"
GenerateHyperlink = MyStr
End Function
sub DefineHy(ThisCell as string, ThisPath as string, ThisString as string)
With ActiveSheet
.Hyperlinks.Add anchor:=Range(ThisCell), Address:=ThisPath, TextToDisplay:=ThisString
End With
End With
End Sub
Which does return a String
and not an HyperLink