Identifying the shape i clicked

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi everyone,
So heres the situation,
I have two shapes
one to called Hide12
the other called Unhide12

i want a macro to identify the shapes name simply by the fact i clicked it,
then use a macro that says.
clicked shape hide,
other shape unhide

OK let me make this clearer,
on my page all my shapes are called either "Hide" or "Unhide" followed by a number for example Hide1 matches Unhide1 etc
i want a macro that i can assinge to all these shapes that says this

Get the Clicked shapes name,
if name starts with "Un" the "Hide" + (number on end of name) visible "Unhide" hidden

so i hide one shape show the other
can this be done?

thanks

Tony
 
Glad we could help & thanks for the feedback.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Yes I know that, but why? It would be easier to have one shape that acts as a toggle & you can change the name every time it's clicked.

I have to admit, I do this quite regularly. I might have a Shape that when I click, hides columns with slicers in it or an area on a sheet where values are entered for a dashboard or whatever. When the area is visible, it is a nicely formatted section in the top left of the worksheet, with a little cross (x) icon/shape in the top right. You click the cross, and the columns disappear and a little tab appears instead. It's beautiful, and it makes the users happy to be working from home day after day with nobody to speak to.

Incidentally, I use it that regularly that I have developed a system for it! I call one shape "myShapeThatDoesActionX_1" and the other "myShapeThatDoesActionX_0". Then I just copy the code below (or similar, I'm at home at the moment, and I don't do it in my personal time, of course) and assign it to both:

VBA Code:
Sub toggleBetweenShapes()
    Dim shpPressed As String
    Dim x As Integer
    Dim shp1 As Shape
    Dim shp2 As Shape
    'get the name of the shape that was pressed
    shpPressed = Application.Caller
    'split the shape name at the underscore, selecting the 1 or 0
    x = Split(shpPressed, "_")(1)
    Set shp1 = ActiveSheet.Shapes(shpPressed) 'shape that was pressed
    Set shp2 = ActiveSheet.Shapes(Replace(shpPressed, x, 1 - x)) '"opposite shape" that was not pressed
    shp1.Visible = shp2.Visible 'make shape one whatever shape 2 is
    shp2.Visible = Not shp2.Visible 'make shape 2 not what shape 2 is
End Sub

Even the code is beautiful (apart from the activesheet bit, I'd nearly always specify). I'd probably add a little check to see if shape one and two had the same visibility in case some wildcat had made them both visible, and some error handling around the non-existence of either shape, but this was just a little snippet to give you an idea. It makes the code reusable and you just have to correctly name your shapes and assign the code to them and hide the first one. I have code for shapes that hide columns ("shpHide_A-C_0" and "shpHide_A-C_1" would hide and unhide columns a to c for example), rows etc based on the name of the shape. One of my favourite shapes is called "shpTakeOverTheInternet_1", but I wouldn't like to divulge what it does. Anyway, thought I'd add my thoughts.

Also, for those looking in that don't know (as I didn't for years), when working with shapes, under the Home tab, Find & Select/Selection Pane is a very useful pane to have visible.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,559
Messages
6,120,194
Members
448,951
Latest member
jennlynn

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