我对VBA Excel有一个疑问。我想知道是否有一种方法可以在移动指针到按钮或单元格上时显示额外的信息。我希望以这种方式提供标签中显示的单词的含义。例如,如果标签显示单词“股票”,我希望某人只需将指针移动到该单词上,然后就会显示信息。
我正在使用Excel 2013,目前考虑的是当指针移动到某个位置时触发事件。
谢谢。
对于单元格,最简单的方法是使用注释。对于按钮,据我所知没有直接的方法。你可以通过在VBA中创建一个函数,在msgbox中显示你想要的信息,例如:
Function exemple()
MsgBox "I ran"
End Function
然后在给定单元格中使用公式调用此函数:
=hyperlink(exemple(),"Displayed text in cell")
如果按钮在用户窗体上,请使用ControlTipText属性。
如果按钮在工作表上,则检测此事件。
Private Sub cmdBtn_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, _
ByVal X As Single, ByVal Y As Single)
If X < cmdBtn.Width And Y < cmdBtn.Height Then
Sheet1.Cells(1, 1).Value2 = "More Info"
End If
End Sub
将按钮的大小设置为两个或更多单元格的大小相同。在您想要放置按钮的位置合并单元格,右键单击并添加超链接。在右侧选择“本文档中的位置”。单击屏幕提示按钮并输入要显示的文本。我建议选择一个空白单元格作为“键入单元格引用”行,然后单击“确定”。右键单击合并的单元格并选择“格式单元格”。将字体大小更改为较大的值,例如28,然后将字体颜色更改为白色。这将防止打印时出现超链接。将按钮移动到合并的单元格上即可完成。无需使用宏。
我希望这对您有效果,就像对我一样。
这是一个宏
Sub ButtonHover()
'
' ButtonHover Macro
'
'
Range("M8:M9").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"'P4P Master'!Y6", ScreenTip:="Button Hover", TextToDisplay:= _
"'P4P Master'!Y6"
With Selection.Font
.Name = "Calibri"
.FontStyle = "Regular"
.Size = 28
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleSingle
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
ActiveSheet.Buttons.Add(786.75, 150.75, 72, 72).Select
Selection.OnAction = "ButtonHover"
ActiveSheet.Shapes("Button 6").IncrementTop -65
ActiveSheet.Shapes("Button 6").ScaleWidth 2.0526315789, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.Shapes("Button 6").ScaleHeight 1.52, msoFalse, msoScaleFromTopLeft
End Sub