using hyperlink to open image file from a button

  • Thread starter Thread starter rayme
  • Start date Start date
R

rayme

I use the hyperlink function to open an image file on the hard disk whose
address is contained in a cell. But can I get this to attach to a button
using a macro or can I open a file whose address is contained in a cell in
some other way.
 
I used a button from the Forms toolbar and placed it on my worksheet.

I assigned it this macro:

Option Explicit
Sub testme()

Dim BTN As Button
Dim testStr As String
Dim myCell As Range

With ActiveSheet
Set BTN = .Buttons(Application.Caller)

Set myCell = ActiveCell
Set myCell = .Cells(BTN.TopLeftCell.Row, "A")
Set myCell = .Range("A1")

testStr = ""
On Error Resume Next
testStr = Dir(myCell.Value)
On Error GoTo 0
If testStr = "" Then
Beep
Else
ActiveWorkbook.FollowHyperlink myCell.Value
End If
End With

End Sub

I wasn't sure what you're doing, so I left these 3 lines in:

Set myCell = ActiveCell
Set myCell = .Cells(BTN.TopLeftCell.Row, "A")
Set myCell = .Range("A1")

Keep one and delete the other two.

Keep the first if you want to use the value in the activecell.
Keep the second if you want to use the value in column A (same row as the
button)
Keep the third if you want to (always) use A1 (of that activesheet).

I'd keep the first if I have a bunch of pictures--just tell the user to select
the cell first and then click the button.

I'd keep the second if I added a bunch of buttons (one per row) and wanted each
button to control one cell/picture (It's ok to assign the same macro to each
button.)

I'd keep the third if I only had one cell/picture to worry about.
 
Back
Top