Help with loop

A

Aaron

I'm not getting the desired result with this loop:

A1 = hello
A2 = goodbye
A3 = hello again

when I run this macro with cells A1-A2 selected, the resulting
hyperlink is http://www.google.com/search?&q=goodbye for all selected
cells.

When I run the macro with A3 selected, it breaks with an error.

Sub makelink()
Dim Cell As Range
For Each Cell In Intersect(Selection, ActiveSheet.UsedRange)
If Cell <> "" Then
Dim sLinkAddress As String
sLinkAddress = "http://www.google.com/search?&q= & Chr(34) &
Cell.Value & Chr(34)
ActiveSheet.Hyperlinks.Add Anchor:=Selection,
Address:=sLinkAddress
Exit For
End If
Next
End Sub
 
P

p45cal

when I run this macro with cells A1-A2 selected, the resulting
hyperlink is =http://www.google.com/search?&q=goodbye for all selected
cells. (Actually I got "http://www.google.com/search?&q=*hello*" and got no
error when just A3 was selected.)
Is this what you're looking for?:Sub makelink()
Dim Cell As Range
For Each Cell In Intersect(Selection, ActiveSheet.UsedRange)
If Cell <> "" Then
Dim sLinkAddress As String
sLinkAddress = "http://www.google.com/search?&q=" & Chr(34) &
Cell.Value & Chr(34)
ActiveSheet.Hyperlinks.Add Anchor:=Cell, Address:=sLinkAddress
End If
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top