ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Help with loop (https://www.excelbanter.com/excel-programming/432909-help-loop.html)

Aaron[_17_]

Help with loop
 
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

p45cal[_81_]

Help with loop
 

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


--
p45cal

*p45cal*
------------------------------------------------------------------------
p45cal's Profile: http://www.thecodecage.com/forumz/member.php?userid=558
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=128987


Aaron[_17_]

Help with loop
 
works great! Thank you!


All times are GMT +1. The time now is 08:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com