vba 将预览悬停在 excel 图像链接上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43923102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hover preview over excel image link
提问by user1663590
I wanted to know is it possible to preview image links by hovering mouse cursor over image urls in excel, or google sheets, or any spreadsheet editor.
我想知道是否可以通过将鼠标光标悬停在 excel、谷歌表格或任何电子表格编辑器中的图像 url 上来预览图像链接。
回答by user1274820
You made me curious, so I looked into this.
你让我好奇,所以我调查了这个。
The answer is, yes - it requires a bit of VBA and is a bit hacky, but here's how you can do it.
答案是,是的 - 它需要一些 VBA 并且有点hacky,但是您可以这样做。
First of all, doing anything on cell hover in excel is a bit hacky.
首先,在 excel 中的单元格悬停上做任何事情都有些笨拙。
To do so, we use the HYPERLINK
formula of a cell.
为此,我们使用HYPERLINK
单元格的公式。
=HYPERLINK(OnMouseOver("http://i.imgur.com/rQ5G8sZ.jpg"),"http://i.imgur.com/rQ5G8sZ.jpg")
=HYPERLINK(OnMouseOver("http://i.imgur.com/rQ5G8sZ.jpg"),"http://i.imgur.com/rQ5G8sZ.jpg")
In this case, I have the URL of a grumpycat picture in my formula.
在这种情况下,我的公式中有一张 grumpycat 图片的 URL。
I also pass this link to a function I create called OnMouseOver
我还将此链接传递给我创建的名为 OnMouseOver
Dim DoOnce As Boolean
Public Function OnMouseOver(URL As String)
If Not DoOnce Then
DoOnce = True
With ActiveSheet.Pictures.Insert(URL)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 75
.Height = 100
End With
.Left = Cells(1, 2).Left
.Top = Cells(1, 2).Top
.Placement = 1
.PrintObject = True
End With
End If
End Function
Finally, in order to clear it when we hover away, we have to put some formulas in the other cells near it.
最后,为了在我们悬停时清除它,我们必须在它附近的其他单元格中放置一些公式。
=HYPERLINK(Reset())
=HYPERLINK(Reset())
And the associated function:
以及相关的功能:
Public Function Reset()
If DoOnce Then
DoOnce = False
ActiveSheet.Pictures.Delete
End If
End Function
Edit
编辑
Expanding on this with multiple links.
使用多个链接对此进行扩展。
We can pass a cell reference along with this to do this with multiple links and have them appear next to the cell.
我们可以同时传递一个单元格引用,以使用多个链接执行此操作,并让它们出现在单元格旁边。
Dim DoOnce As Boolean
Public Function OnMouseOver(URL As String, TheCell As Range)
Reset
If Not DoOnce Then
DoOnce = True
With ActiveSheet.Pictures.Insert(URL)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 300
.Height = 200
End With
.Left = Cells(TheCell.Row, TheCell.Column + 1).Left
.Top = Cells(TheCell.Row, TheCell.Column + 1).Top
.Placement = 1
.PrintObject = True
End With
End If
End Function
Public Function Reset()
If DoOnce Then
DoOnce = False
ActiveSheet.Pictures.Delete
End If
End Function