Mouseover Event Question

TheXxvI

New Member
Joined
Jan 14, 2015
Messages
12
Hello,

I have Label1 and Label2 on a UserForm. By default, Label1 will be visible and Label2 will not be visible.

I need a code that will make Label2 visible and Label1 not visible when I hover over Label1, but also needs to make Label1 visible and Label2 not visible again when I move the mouse off of Label1.

Finally, I plan to repeat the code for Labels 3 and 4, 5 and 6, etc...

Thank you.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
The best (and only) way I can think to do it, since you don't have a "MouseOver" event, is to use the X and/or Y coordinates of your existing labels to tell the code when to hide or show, based on the "MouseMove" command on the USERFORM itself, not the labels.

Something like this works for me.

Code:
Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

With UserForm1
    If X > 111 And X < 186 Then
        .Label1.Visible = False
        .Label2.Visible = True
    Else
        .Label1.Visible = True
        .Label2.Visible = False
    End If
    
End With
    
End Sub


Keep in mind though, the coordinates I put in (186, 11, etc.) are based on where the labels are on my userform. Yours will almost certainly be different.

You can use the msgbox or another label/text box, etc., to determine what your X/Y coordinates are, then plug in those values to the code.
 
Upvote 0
This is a bit easier, since it checks the needed position and size of label 1:

Code:
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  With Me.Label1
    If X > .Left And X < .Left + .Width And Y > .Top And Y < .Top + .Height Then
      .Visible = False
    Else
      .Visible = True
    End If
    Me.Label2.Visible = Not .Visible
  End With
End Sub
 
Upvote 0
The userform MousMove event will not fire when the mouse is over the label .. Tracking the mouse pointer (using the GetCursorPos API) inside a loop and periodically checking when it is within the label rectangle is very accurate and doesn't rely on the unsound form events for this purpose .. But first , converting the label Letf ,Top, width & Height values from Points to Pixels is required

EDIT:
I recall doing something similar some years ago with worksheet controls .. I'll try to find the code and see if I can adapt it to work for this userform labels
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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