VBA to add a screentip to a macro-enabed button

scotthannaford1973

Board Regular
Joined
Sep 27, 2017
Messages
114
Office Version
  1. 2010
Platform
  1. Windows
Hi all

I am using a shape (Rectangle 9) as a button with a macro assigned to it (ExpandFuture) and I'd like to add a screen-tip for when people hover over the button. So just basic text... but importantly the macro still has to run when the click the button. Have found a few suggestions but none of them work!
 
You're better off not using ActiveX controls on worksheets. They're unstable and it's asking for trouble, if you want to see what I mean, google ActiveX resizing on worksheet
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
@scotthannaford1973

If you share @Kyle123's concerns...
- the solution in post#10 works equally well with Form Control buttons
- but there is less flexibility over the look of those buttons
 
Upvote 0
How about this :

The code below assumes the shape is Rectangle 9 and it is located in Sheet1 .. Change these as required.

Code in the ThisWorkbook Module:
Code:
Option Explicit

Private WithEvents cmb As CommandBars

Private Type POINTAPI
    x As Long
    y As Long
End Type

[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If"]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else"]#Else[/URL] 
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
[URL="https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End"]#End[/URL]  If

Private Sub Workbook_Open()
    Call AddToolTipToShape(Shp:=Sheet1.Shapes("Rectangle 9"), _
    ScreenTip:="This is tooltip for shape : 'Rectangle 9'" & vbCrLf & vbCrLf & "Click Shape to run Macro.")
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Call AddToolTipToShape(Shp:=Sheet1.Shapes("Rectangle 9"), _
    ScreenTip:="This is tooltip for shape : 'Rectangle 9'" & vbCrLf & vbCrLf & "Click Shape to run Macro.")
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call CleanUp
End Sub

Private Sub AddToolTipToShape(ByVal Shp As Shape, ByVal ScreenTip As String)
    On Error Resume Next
    Shp.Parent.Hyperlinks.Add Shp, "", "", ScreenTip:=ScreenTip
    Shp.AlternativeText = Shp.AlternativeText & "-ScreenTip"
    Set cmb = Application.CommandBars
End Sub

Sub CleanUp()
    Dim ws As Worksheet, Shp As Shape
    On Error Resume Next
    For Each ws In Me.Worksheets
        For Each Shp In ws.Shapes
            If InStr(1, Shp.AlternativeText, "-ScreenTip") Then
                Shp.Hyperlink.Delete
                Shp.AlternativeText = Replace(Shp.AlternativeText, "-ScreenTip", "")
            End If
        Next Shp
    Next ws
End Sub

Private Sub cmb_OnUpdate()
    Dim tPt As POINTAPI
    GetCursorPos tPt
    If InStr(1, "RangeNothing", TypeName(ActiveWindow.RangeFromPoint(tPt.x, tPt.y))) = 0 Then
        If ActiveWindow.RangeFromPoint(tPt.x, tPt.y).OnAction <> "" Then
            If GetAsyncKeyState(vbKeyLButton) Then
                Application.Run (ActiveWindow.RangeFromPoint(tPt.x, tPt.y).OnAction)
            End If
        End If
    End If
End Sub

The code should enable the shape to display a ScreenTip when the user hover over it while still permitting to run the associated Macro ExpandFuture

cheers, Jaafar - works like a dream (with a bit of personalisation :))
 
Upvote 0
Some years later, I had a need for this, and it worked like a charm on my own code :biggrin: Thanks a lot !
 
Upvote 0

Forum statistics

Threads
1,216,189
Messages
6,129,411
Members
449,509
Latest member
ajbooisen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top