Forum Discussion
Trying to toggle colour of shapes on mouse click
- Jan 17, 2021
You need only a single macro that you assign to all the shapes:
Sub ChangeFill() With ActiveSheet.Shapes(Application.Caller).Fill .Visible = Not .Visible End With End Sub
Please note that when the interior of a shape is transparent, you'll have to click on its edge to run the macro; clicking inside the shape won't work (precisely because it is transparent).
You need only a single macro that you assign to all the shapes:
Sub ChangeFill()
With ActiveSheet.Shapes(Application.Caller).Fill
.Visible = Not .Visible
End With
End Sub
Please note that when the interior of a shape is transparent, you'll have to click on its edge to run the macro; clicking inside the shape won't work (precisely because it is transparent).
- nic-nicolJan 17, 2021Copper Contributor
Wow! Thanks! I'm new to all this (just learning Python as first language and this is my first attempt at VB) and knew there'd be a simple, more elegant solution than my clumsy, long winded one!
Just one problem: I'm getting a run time error on this line: item with specified name not found
With ActiveSheet.Shapes(Application.Caller).Fill
Presumably I need to insert the sheet or workspace in there somewhere? Tell it where to look?
Pity the 'click area' can't be modified, that might be problematic given the small size of some of the shapes, I'm thinking add an extra shape/button off to the side as an undo/clear all feature? More research!
Thanks again, you've been a great help so far 🙂
- HansVogelaarJan 17, 2021MVP
Make sure that each of your shapes has a unique name.
- nic-nicolJan 17, 2021Copper Contributor
I've got it now!
made a rookie mistake in my haste: hadn't right clicked each shape and assigned it a macro!
That and naming them in the name manager has solved all my problems!
simple but very effective bit of code, you're a genius sir! I'm really grateful!
Later I'll look at a reset shape and maybe making it grey not black, but that's the next stage!
Thanks again!