Change the colour of a shape on a worksheet on mouseover

450nick

Well-known Member
Joined
May 11, 2009
Messages
507
Hi Chaps,

I would like to make some smart looking buttons in a frozen pane to control a filter range, and as part of that I'd like them to change colour on mouseover and when active. I know how to do this in a userform but how would I do this with a worksheet object?
 

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.
Hi,
You can use MouseMove event of command button for that

Code:
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If Sheets("Sheet1").Range("A1").Value = 10 Then
   CommandButton1.BackColor = RGB(255, 0, 0)
   Else
   CommandButton1.BackColor = RGB(0, 255, 0)
End If
End Sub

Hope it helps.
 
Upvote 0
Hi, thanks for the code, I tried taking out the if statement to see if I can just get the box to change colour on mouseover and it doesn't seem to do anything - how to I make the subroutine activate?
 
Upvote 0
Hi,
Make sure that your button name is CommandButton1 for the code to work if not just go to VBA and select your button from top left you'll be able to see the events on top right corner you will have to select mouse move event from there and put the code from If to Endif in that event.
Hope it will work out.
 
Upvote 0
I tried substituting the CB1 for my button name - I think the issue is that it isn't showing up in the dropdown in the VBA editor - any idea why?? I'm using a shape - not a proper button. Although there is also a button on the sheet and that doesn't show up either!
 
Upvote 0
Shapes and Form controls don't have events - only ActiveX objects do.
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,543
Members
449,089
Latest member
davidcom

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