我有以下功能,将显示图片,如果你运行你的鼠标在它上面。它非常整洁,效果很好。但是,我想将它从鼠标移动到双击功能。
下面是当您在单元格上运行鼠标时显示图片的代码:
Dim DoOnce As Boolean
Public Function OnMouseOver(URL As String, TheCell As Range)
DoOnce = True
With ActiveSheet.Pictures.Insert(URL)
With .ShapeRange
.LockAspectRatio = msoTrue
.Width = 570
.Height = 380
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 Function我知道我需要下面的代码,如果你双击它,就会发生一些事情:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address = "$A$1" Then
'do something
End If
Cancel = True
End Sub我似乎不能将两者合并-这样我就可以获得原始代码的功能,但需要双击,而不是仅仅将鼠标移到单元格上。
初始代码按进程的顺序在我的计算机上的图片中引用连接的图片查找地址。随着更多图片的生成,我用公式复制了新的链接,这是快速、简单和容易的,这就是为什么我想保留初始代码的"OnMouseOver“函数,这样我就可以轻松地将其复制到新的单元格中。我只想通过双击来显示图片,而不是像现在这样在单元格上运行鼠标。
如果我能帮上忙那就帮了大忙了。
谢谢,
史蒂夫
发布于 2020-06-08 22:21:44
当“双击”进入excel单元格的编辑时,可能会导致对现有值/公式进行不必要的编辑。所以,我推荐使用Worksheet_SelectionChange event。你也可以通过导航/箭头键来使用它。
将以下代码粘贴到与VBA相关的"Sheet“对象中
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发布于 2020-06-12 13:33:07
https://stackoverflow.com/questions/62256420
复制相似问题