Double click a cell to show a picture from a link in excel with VBA

前端 未结 2 1930
清酒与你
清酒与你 2021-01-15 15:01

I have the following function that will display a picture if you run your mouse over it. It\'s quite neat and works well. However, I\'d like to change it from running your m

相关标签:
2条回答
  • 2021-01-15 15:29

    As "Double Click" enters into the editing of the excel cell, it may cause undesired edit of existing values/ formulas. So, I would recommend Worksheet_SelectionChange event. You can use it with navigation/ arrow keys as well.

    Paste the following code in VBA - relevant "Sheet" Object

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim url As String
    
    On Error Resume Next 'to avoid error if the target cell does not have url
    'Even if we can use URLExists function to avoid such error,
    'there will still be errors when there is value but not recognised by the function.
    'For example an error caused by function =100/0
    
    ActiveSheet.Pictures.Delete
    
    'If URLExists(Target.Value) Then
    'As we are using "on error resume next statement we don't need this.
    'here is a link to this function
    'https://stackoverflow.com/a/25428811/9808063
    
    url = Target.Value
    
    With Me.Pictures.Insert(url)
        With .ShapeRange
            .LockAspectRatio = msoTrue
            .Width = 300
            .Height = 200
        End With
        .Left = Cells(Target.Row + 2, Target.Column + 1).Left
        .Top = Cells(Target.Row + 2, Target.Column + 1).Top
        .Placement = 1
        .PrintObject = True
    End With
    'End If
    End Sub
    
    0 讨论(0)
  • 2021-01-15 15:32

    Answer to this question can be found at the link below.

    https://www.mrexcel.com/board/threads/double-click-a-cell-to-show-a-picture-from-a-link.1136465/page-2

    0 讨论(0)
提交回复
热议问题