CommandButton MouseMove

breynolds0431

Active Member
Joined
Feb 15, 2013
Messages
303
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello:

I have the following set up to change my CommandButton upon mouseover. But what I would like is for the command button to return to the original fore/back colors after mouse-off. Is this possible? I am also not using a UserModule. Just a commandbox set to be visible based on another cell's value. FYI...

Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
CommandButton1.ForeColor = RGB(191, 191, 191)
CommandButton1.BackColor = RGB(31, 78, 120)

End Sub


Thank you in advance!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
The following solution uses a hidden label to achieve the desired effect...

1) Add a label to the userform, making sure that the size of the label is greater than the commandbutton on all sides.

2) Set the BackStyle property for the label to fmBackStyleTransparent in the Properties window.

3) Remove the caption from the label in the Properties window.

4) Position the label over the commandbutton, making sure that the commandbutton is completely hidden.

5) Hide the label behind the commandbutton (right-click the label, and select 'Send Backward').

6) Place the following code in the userform code module...

Code:
Option Explicit

Dim nForeColor As Long
Dim nBackColor As Long
    
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)

    CommandButton1.ForeColor = RGB(191, 191, 191)
    CommandButton1.BackColor = RGB(31, 78, 120)

End Sub

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

    CommandButton1.ForeColor = nForeColor
    CommandButton1.BackColor = nBackColor
    
End Sub

Private Sub UserForm_Initialize()

    nForeColor = CommandButton1.ForeColor
    nBackColor = CommandButton1.BackColor

End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,215,172
Messages
6,123,428
Members
449,099
Latest member
COOT

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