简体   繁体   中英

How to display some more information when moving the mouse pointer over a button in VBA excel.

I have a doubt concerning vba excel. I would like to know if there is a way to show extra information when you move the pointer over a button or cell. I want to do it as a way to provide the meaning of the words that are displayed in a label. For example if the label shows the word "stock" I want that someone could just move the pointer over the word and then it shows the info.

Im using Excel 2013 and so far Im thinking in a event when the pointer moves over a position.

Thanks.

For cells, the easiest is to use comments. For buttons, there is no direct way as far as I'm aware. What you could do to go around that is make a function in vba that displays the info you want in a msgbox, for exemple:

Function exemple()
MsgBox "I ran"
End Function

And the call this function by using the formula in a given cell:

=hyperlink(exemple(),"Displayed text in cell")

Now, whenever you hover that cell, the function will display the msgbox. You can then place the button over the cell (make sure that the button is smaller than the cell or input the formula in multple cells around the button or else it function wont run). You could also change the function to hide/display comments instead of a msgbox, which can get anoying when it pops up everytime you want to click the button.

It is far from perfect, but should do the trick.

Edit: I suggest you leave the "Displayed text in cell" in the formula to "", because otherwise, only hovering the text will run the function, where as if you leave it blank, the whole cell will run the formula.

If the button is on a UserForm use the ControlTipText property

If the button is on a sheet this detects the event

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

It isn't a mouse hover -- but if you want to provide additional information and you don't like the annoying triangle that displays when a cell has a comment, you can put the additional information as the input message for data validation for the cell. Then if someone selects the cell the information will be displayed. You can also use the worksheet's BeforeRightClick event to display information when a cell is right-clicked on.

Make the button the same size as two or more cells. Merge cells together where you want the button to be, right click and add a hyperlink. On the Right choose Place in This Document. Click the ScreenTips button and enter the text you want to display. I recommend Choosing a blank cell for the Type the cell reference line and click OK. Right click the merged cells and select Format Cells. Change the font size to something large, I like 28, then change the font color to white. This will prevent the hyperlink from showing up when printing. Move your Button over the merged cells and there you have it. No macros needed.

I hope this works for you as it does for me.

Here's a macro

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM