• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Programatically adding hyperlinks to images [SOLVED]

Nostalgia80

New Member
Hello,


I have a spreadsheet that lists product data. Each row item (individual product) lists a unique ID. At the end of each row item, I have used a hyperlink formula to create a url....


EX: =HYPERLINK("http://www.example.com/product.aspx?ID="&F3&"&target=ts2", ""&A3&"")


Next to this hyperlink formula cell, I have inserted an image....in this case, an image that looks like a 'Buy Now' or 'Add to Cart' button.


What I would like to do is connect the URL generated from the 'hyperlink formula' to the button image. Is this possible with VBA? I have around 300 or so products, so I would like to do this in the quickest way possible.


Thank you for any help you can offer!
 
Hi, Nostalgia80!


First of all welcome to Chandoo's website Excel forums. Thank you for your joining us and glad to have you here.


As a starting point I'd recommend you to read the green sticky topics at this forums main page. There you'll find general guidelines about how this site and community operates (introducing yourself, posting files, netiquette rules, and so on).


Among them you're prompted to perform searches within this site before posting, because maybe your question had been answered yet.


Feel free to play with different keywords so as to be led thru a wide variety of articles and posts, and if you don't find anything that solves your problem or guides you towards a solution, you'll always be welcome back here. Tell us what you've done, consider uploading a sample file as recommended, and somebody surely will read your post and help you.


And about questions in general...


If you haven't performed yet the search herein, try going to the topmost right zone of this page (Custom Search), type the keywords used in Tags field when creating the topic or other proper words and press Search button. You'd retrieve many links from this website, like the following one(s) -if any posted below-, maybe you find useful information and even the solution. If not please advise so as people who read it could get back to you as soon as possible.


And about this question in particular...


Give a look at this file:

https://dl.dropboxusercontent.com/u/60558749/Adding%20Hyperlinks%20to%20Images%20%28for%20Nostalgia80%20at%20chandoo.org%29.xlsm


It works under the following assumptions/constraints/conditions:


a) Each image should start (top left border) within a cell of the row from which its link is gonna be assigned, i.e., images cannot start in previous rows (I assume that neither in later as it won't have sense, I guess).


b) It'll work on all shapes for which the name is equal or different from what specified in these constants: Like, ksPatternInclude; Not like, ksPatternExclude.

So for including all images set them to respectively "*" and "", for including all except command buttons (defined as cmdXXXX) set them to "*" and "cmd*", for including all with "Picture" in the name only set them to "*Picture*" and "", ...


c) All link addresses must be in the same column, specified by constant kiLinkColumn, and maybe absent and then that particular image won't get a link.


This is the code behind the macro:

-----

[pre]
Code:
Option Explicit

Private Sub cmdAddLinksToImages_Click()
' constants
Const ksPatternInclude = "*Picture*"
Const ksPatternExclude = "cmd*"
Const kiLinkColumn = 3
' declarations
Dim I As Long, J As Long, S As String, bOk As Boolean
' start
' process
With ActiveSheet
For I = 1 To .Shapes.Count
S = .Shapes(I).Name
bOk = (S Like ksPatternInclude) And Not (S Like ksPatternExclude)
If bOk Then
J = .Shapes(I).TopLeftCell.Row
If .Cells(J, 1).Value <> "" Then
If .Cells(J, kiLinkColumn).Value <> "" Then
.Hyperlinks.Add .Shapes(I), .Cells(J, kiLinkColumn).Value
End If
Else
Exit For
End If
End If
Next I
End With
' end
Beep
End Sub
[/pre]
-----


Just advise if any issue.


Regards!


EDITED:


PS: Looking at the code I now realize that the sort part could be omitted (that happens when one extracts things from other projects) :(

PS2: And the load too, then? Let me check it.


EDITED:


PS3: Yes, file uploaded again and shortened code updated.
 
A big THANK YOU to you SirJB7! This was exactly what I was looking for and it works perfectly.


Also, thanks for the welcoming tips...I'll be sure to take your advice while browsing around this site!
 
Hi, Nostalgia80!

Glad you solved it. Thanks for your feedback and for your kind words too. And welcome back whenever needed or wanted.

Regards!
 
Back
Top