View Full Version : turn strings into hyperlinks with VBA


ajetrumpet
10-23-2009, 09:51 AM
hello all,

I have this code:Function test()

Dim r As Range

For Each r In Range("a1", "a905")
If InStr(r, "http://") > 0 Or InStr(r, "www.") > 0 Then
Range(r.Address).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, _
Address:="www.google.com", _
TextToDisplay:="www.google.com"
End If
Next r

End Functionthe function works with literal strings for address and text to display, but if I use the 'R' variable in place of the strings, I get a runtime error 5, invalid call or argument. the args for address and TTD are strings, so I have tried cstr(r) in both and I still get the error. Can someone help me out here? I need to use the 'R' or 'R.VALUE' to turn the current cell's value into a hyperlink with the same text to display. thanks!

chergh
10-23-2009, 11:20 PM
Works fine for me when written like this:


Function test()
Dim r As Range

For Each r In Range("a1", "a905")
If InStr(r, "http://") > 0 Or InStr(r, "www.") > 0 Then
Range(r.Address).Select
ActiveSheet.Hyperlinks.Add Anchor:=Selection, _
Address:=r.Value, _
TextToDisplay:=r.Value
End If
Next r

End Function