# Changing button colours using MouseMove Sub

#### Sam1234

##### New Member
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

### Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

#### SIXTH SENSE

##### Well-known Member
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
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
The button is actually on a worksheet within excel rather than on a user form.

#### jimboy

##### Well-known Member
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
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

Replies
4
Views
140
Replies
3
Views
2K
Replies
1
Views
486
Replies
9
Views
545
Replies
2
Views
239

1,127,809
Messages
5,627,019
Members
416,215
Latest member
Ostie3994

### 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.

### Which adblocker are you using?

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

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