SOLVED

Trying to toggle colour of shapes on mouse click

Copper Contributor

I'm putting shapes over a picture of a human body so you can click an area, say upper left leg, and it's highlighted, click again returns to transparent so you just see the picture below.

 

I've got some VBA code that works for the first shape, but not after that.

I'm inserting the shape, Oval 3 > going to visual basic > insert module > pasting code > changing name of sub to changefilloval3() > changing the object in the code to "Oval 3" > debug > run > back to excel > right click shape > assign macro >  changefilloval3

 

Then it works fine, until I add the next shape and module!!

 

I'm inserting the shape, Oval 4 > going to visual basic > insert module > pasting code > changing name of sub to changefilloval4() > changing the object in the code to "Oval 4" > debug > run > back to excel > right click shape > assign macro >  changefilloval4

 

then neither of them work and it's virtually impossible to click on them!

also when it does work, the size of the shape you can click is tiny, is there any way to make the 'click area' the same size as the shape?

 

am I going about this the wrong way?

should it be one macro for all, not one for each shape? they need to act independently.

am I just getting the coding wrong?

any help greatly appreciated.

here's the code:

 

 

  1. Sub changefill()
  2.  
  3.  
  4. ActiveSheet.Shapes("Oval 1").Select 'change oval 1 to whatever your shape is called
  5. If Selection.ShapeRange.fill.Visible = msoFalse Then
  6. Selection.ShapeRange.fill.Visible = msoTrue
  7. Selection.ShapeRange.fill.Solid
  8. Selection.ShapeRange.fill.ForeColor.SchemeColor = 8
  9. Else
  10.  
  11. Selection.ShapeRange.fill.Visible = msoFalse
  12.  
  13.  
  14.  
  15.  
  16. End If
  17. End Sub

 

 

12 Replies
best response confirmed by nic-nicol (Copper Contributor)
Solution

@nic-nicol 

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

@Hans Vogelaar 

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 :)

 

@nic-nicol 

Make sure that each of your shapes has a unique name.

@Hans Vogelaar 

aren't they assigned a unique name when created? Oval 3, Oval 4, Trapeziod 5, etc?

I've gone into formulas > define name and assigned names but that's not doing anything.

@Hans Vogelaar 

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!

Have set it up with nice fill colours, outline etc, all working perfectly.

only thing is, you mentioned I can't alter the size/position of the click area for each shape, is it possible to make the click area visible?

I am doing a behaviour data chart and there are 3 behaviours listed in the data section. However, when the graph section appears to show the legend, the legend only shows two of the behaviours.

@nic-nicol 

The clickable area of a shape is the visible part:

  • If the border is visible, the border is clickable.
  • If the border is transparent, it is not clickable.
  • If the interior is visible, the interior is clickable.
  • If the interior is transparent, it is not clickable.

So what you see is what you get...

@Dr_Woodson1964 

That appears to be an entirely different question. Please start a new question (topic/thread).

Dr_woodson1964@Hans Vogelaar 

 

that was my question

@Dr_Woodson1964 

You have asked an entirely unrelated question in someone else's thread. That is not how a forum should work - it will become very difficult for users searching for help if different users ask wildly different questions in the same thread.

Please go back to https://techcommunity.microsoft.com/t5/excel/bd-p/ExcelGeneral and click 'Start a New Discussion', then post your question there. Thanks in advance!

@Hans Vogelaar 

 

Gotcha!! Solved it by increasing the outline of the shape to make it easier to hit.

Thanks again :)

1 best response

Accepted Solutions
best response confirmed by nic-nicol (Copper Contributor)
Solution

@nic-nicol 

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

View solution in original post