Run a macro from inside a macro - based on the selected shape name - selected by the current macro

Jamie McMillan

Board Regular
Joined
Nov 8, 2021
Messages
169
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have Shapes named ,1, 2, 3, 4
The Macros assigned to each shape are called, 1, 2, 3, 4

Another shape called A, 'macro called A', selects one of these shapes, based on a cell value, Range("Q2").Value, of (1, 2, 3 or 4)

If the macro selects shape 1 I want it to then Call 1 (Call Macro 1), If the macro selects shape 3 I want it to then Call 3 (Call Macro 3)

Sub onetwo()

Dim Mac As Variant (I have tried using, long, string, ... etc)
Set Mac = ActiveSheet.Shapes(Application.Caller).Name (I have tried not using Set - just Mac = ActiveSheet.Shapes(Application.Caller).Name )

If Range("Q2").Value = 1 Then
ActiveSheet.Shapes.Range(Array("1")).Select
Selection.ShapeRange.ZOrder msoBringToFront
Else
If Range("Q2").Value = 2 Then
ActiveSheet.Shapes.Range(Array("2")).Select
Selection.ShapeRange.ZOrder msoBringToFront
Else
If Range("Q2").Value = 3 Then
ActiveSheet.Shapes.Range(Array("3")).Select
Selection.ShapeRange.ZOrder msoBringToFront
Else
If Range("Q2").Value = 4 Then
ActiveSheet.Shapes.Range(Array("4")).Select
Selection.ShapeRange.ZOrder msoBringToFront
End If
End If
End If
End If

Call Mac
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Use of the Application.Caller property would not be useful in this case because it would always return the same value, i.e. the name of the button or shape which was pressed to run this macro.

The code below does exactly as what you're trying to do. Note that, as in your code, there is no check anywhere whether the shape in question exists and/or whether a macro has been assigned to that shape. Hope this helps.

VBA Code:
Sub JamieMcMillan()
    
    Dim ShpName As String, Shp  As Shape

    ShpName = Range("Q2").Value
    Select Case ShpName
        Case 1, 2, 3, 4
        Set Shp = ActiveSheet.Shapes(ShpName)
        With Shp
            .ZOrder msoBringToFront
            .Select
            Application.Run .OnAction
        End With
    End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,977
Messages
6,175,753
Members
452,667
Latest member
vanessavalentino83

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