Changing button colours using MouseMove Sub

Sam1234

New Member
Joined
Jun 30, 2003
Messages
41
Here is an interesting one...

Using the following code, when you move the mouse over the button it changes colour.

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

But how can I adapt this code so that the colour changes back again once the mouse has moved off the button?

Presumably this has something to do with the X, Y values..?

Sam
 

Some videos you may like

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

SIXTH SENSE

Well-known Member
Joined
Oct 29, 2003
Messages
1,883
hi!
this toogles the button background from green to violet.
But fast motion of the mouse sometimes miss the coordanate!
I wonder if somebody can correct this to be exact that the mouse is not on top of the button!!!

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton2_MouseMove(<SPAN style="color:#00007F">ByVal</SPAN> Button <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>)
<SPAN style="color:#00007F">If</SPAN> Y <= 5 <SPAN style="color:#00007F">Or</SPAN> Y >= CommandButton2.Height - 5 <SPAN style="color:#00007F">Or</SPAN> X <= 5 <SPAN style="color:#00007F">Or</SPAN> X >= CommandButton2.Width - 5 <SPAN style="color:#00007F">Then</SPAN>
CommandButton2.BackColor = 4966415 <SPAN style="color:#007F00">'green</SPAN>
<SPAN style="color:#00007F">Else</SPAN>
CommandButton2.BackColor = &HFF8080
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
</FONT>
 

jimboy

Well-known Member
Joined
Apr 11, 2002
Messages
2,314
You could change it back when you move over the userform;

Code:
Private Sub UserForm_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.BackColor = -2147483633
End Sub
 

Sam1234

New Member
Joined
Jun 30, 2003
Messages
41
The button is actually on a worksheet within excel rather than on a user form.
 

jimboy

Well-known Member
Joined
Apr 11, 2002
Messages
2,314
SIXTH SENSE said:
hi!
this toogles the button background from green to violet.
But fast motion of the mouse sometimes miss the coordanate!
I wonder if somebody can correct this to be exact that the mouse is not on top of the button!!!

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton2_MouseMove(<SPAN style="color:#00007F">ByVal</SPAN> Button <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Shift <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Integer</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> X <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>, <SPAN style="color:#00007F">ByVal</SPAN> Y <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Single</SPAN>)
<SPAN style="color:#00007F">If</SPAN> Y <= 5 <SPAN style="color:#00007F">Or</SPAN> Y >= CommandButton2.Height - 5 <SPAN style="color:#00007F">Or</SPAN> X <= 5 <SPAN style="color:#00007F">Or</SPAN> X >= CommandButton2.Width - 5 <SPAN style="color:#00007F">Then</SPAN>
CommandButton2.BackColor = 4966415 <SPAN style="color:#007F00">'green</SPAN>
<SPAN style="color:#00007F">Else</SPAN>
CommandButton2.BackColor = &HFF8080
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
</FONT>

I've amend this code (thanks) so it changes to Grey, but is the mouse movement is fast...

...you could also add the worksheet_selectionchange to change it back to Grey.

Code:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Y <= 5 Or Y >= CommandButton1.Height - 5 Or X <= 5 Or X >= CommandButton1.Width - 5 Then
CommandButton1.BackColor = -2147483633
Else
CommandButton1.BackColor = &HFF8080
End If

End Sub

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If CommandButton1.BackColor = &HFF8080 Then
CommandButton1.BackColor = -2147483633
End If
End Sub
 

Sam1234

New Member
Joined
Jun 30, 2003
Messages
41
That works quite well, though its a bit tempremental isn't it! Doesn't always want to change back when you move off it.

Sam
 

Watch MrExcel Video

Forum statistics

Threads
1,122,865
Messages
5,598,530
Members
414,245
Latest member
Major Aly

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
Top