Active X buttons

Asked

Viewed 34 times

-1

I have a button (Activex - Commandbutton) on a spreadsheet that I would like to change color by hovering the mouse up.

I did the following the code below, it even changes color but does not return the previous color:

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

CommandButton1.BackColor = &H8000000D

End Sub

How to change color when just hover the mouse over?

  • Hello! This button is on a form?

2 answers

0


Hi, @Hudson!

The solution is a little simpler that it seems.

Just put a transparent Label on the back, slightly larger than the button can be 2 pixels for each side.

This label will be Visible = true but backstyle = 0 - Frmbackstyletransparent

And then you enter the following code:

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.BackColor = &H8000000D
End Sub

Private Sub Label1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.BackColor = &H8000000F
End Sub

The trick here is that by placing the label larger than the button, as soon as the mouse enters the Label automatically means that it crossed one of the button borders, then you reset the color : )

This works on both Sheets and userform and you don’t depend on any more events

  • topppp thanks!!

0

Hello! I believe it wasn’t exactly what you wanted, but the most I could do was this::

When passing the mouse, change the color as you did.

Then, by clicking back on any cell of the sheet, using the Worksheet_selection Change, the button return to the original color or any other preferred color:

Sub changecolor(bt As CommandButton)

    bt.BackColor = &H8000000D

End Sub

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

Call changecolor(CommandButton1)

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

CommandButton1.BackColor = &H8000000F
CommandButton1.ForeColor = &H80000012

End Sub

I hope you at least get closer to what you need.

  • Thank you!!! Helped and very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.