Is it possible to list all macros that are assigned to shapes?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I have a large document and I'm trying to do a bit of a clean up,
I was woundering if there was a macro that could give me a list of all macros that are assigned to a shape within the document?

I use shapes as buttons so this would help me see which macros are currently used buy a button?

If possible this would be perfect
Create a new sheet
Column A show Button Name,
Column B show sheet that buttons on
Column C macro name


anything would be a great help

Thanks

tony
 

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.
The following macro assumes that an Excel workbook is active, and lists all macros assigned to shapes within the active workbook...

VBA Code:
Option Explicit

Sub ListMacrosAssignedToShapes()

    Dim list() As String
    Dim sht As Object
    Dim shp As Object
    Dim temp As Variant
    Dim cnt As Long
   
    ReDim list(1 To 100, 1 To 3)
   
    cnt = 0
    For Each sht In ActiveWorkbook.Sheets
        For Each shp In sht.Shapes
            If Len(shp.OnAction) > 0 Then
                cnt = cnt + 1
                list(cnt, 1) = shp.Name
                list(cnt, 2) = sht.Name
                list(cnt, 3) = shp.OnAction
                If cnt Mod 100 = 0 Then
                    temp = Application.Transpose(list)
                    ReDim Preserve temp(1 To 3, 1 To cnt + 100)
                    list = Application.Transpose(temp)
                End If
            End If
        Next shp
    Next sht
   
    If cnt > 0 Then
        Worksheets.Add
        Range("A1:C1").Value = Array("Button Name", "Sheet Name", "Macro Name")
        Range("A2").Resize(UBound(list, 1), UBound(list, 2)).Value = list
    Else
        MsgBox "No shapes with macros found!", vbExclamation
    End If
   
End Sub

Hope this helps!
 
Upvote 0
Solution
@Domenic,

I would like to ask you, if your code can be modified, so that, when clicking in a cell in column C (where the names of the VBA codes are listed) to open the respective module (not to run it).
Thank you.
 
Upvote 0
@Domenic,

I would like to ask you, if your code can be modified, so that, when clicking in a cell in column C (where the names of the VBA codes are listed) to open the respective module (not to run it).
Thank you.

The following event handler is executed whenever a cell in Column C is double-clicked. Note that it assumes that the first row in Column C contains a header. Also, note that the code must be placed in the code module for your sheet. So you'll need to right-click the sheet tab, select View Code, and then copy/paste the code into the code module.

The code loops through each module within the workbook, and searches for a macro within the module for the name found in the double-clicked/target cell. If found, it opens the Visual Basic Editor, if not already opened, and then it shows the code pane for that module. If not found, it displays a message accordingly. Note that the code searches for an exact match and is not case-sensitive. However, these settings can be changed as desired.

Also, you'll need to allow access to the VBA project object module . . .

VBA Code:
File >> Options >> Trust Center >> Trust Center Settings >> Developer Macro Settings >> select Trust access to the VBA project object model

Here's the code...

VBA Code:
Option Explicit

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

    If Not Intersect(Target, Columns("C")) Is Nothing Then
        If Target.Row > 1 Then
            If Len(Target) > 0 Then
                Cancel = True
                Dim vbComp As Object
                Dim sl As Long, sc As Long, el As Long, ec As Long
                Dim found As Boolean
                For Each vbComp In ThisWorkbook.VBProject.VBComponents
                    With vbComp.CodeModule
                        found = .Find(Target:=Target.Value, StartLine:=sl, StartColumn:=sc, EndLine:=el, EndColumn:=ec, wholeWord:=True, MatchCase:=False, PatternSearch:=False)
                        If found Then
                            Application.VBE.MainWindow.Visible = True
                            .CodePane.Show
                            Exit Sub
                        End If
                    End With
                Next vbComp
                MsgBox Target.Value & " not found!", vbExclamation
            End If
        End If
    End If
 
End Sub

Hope this helps!
 
Last edited:
Upvote 0
Hi Domenic,

Tried your code (put in sheet module), nothing happens, on double click in column C. Tried with new file and with my file. What I do wrong?
Please, can you tell me whats wrong?
Thank you.
 
Upvote 0
My apologies, somehow I went off track, and didn't give you what you actually asked for. So I have amended my post. Let me know if you have any questions or run into any problems.

Cheers!
 
Upvote 0
Thanks for reply.

Domenic,

Because the first code writes in column C, the file name and then the module name, the BeforeDoubleClick event gives the message that that module was not found.
I deleted the file name from column C and when I double-click in one of the cells, the respective module opens.
How can I change the VBA code "ListMacrosAssignedToShapes" so that it no longer writes the file name in column C.
Thank you.
 
Upvote 0
Oh, I didn't know that you would be using "ListMacrosAssignedToShapes" together with the BeforeDoubleClick event handler. In any case, to exclude the filename from the macro names, simply replace . . .

VBA Code:
list(cnt, 3) = shp.OnAction

with

VBA Code:
list(cnt, 3) = Mid(shp.OnAction, InStr(1, shp.OnAction, "!") + 1)

Cheers!
 
Upvote 0

Forum statistics

Threads
1,215,371
Messages
6,124,529
Members
449,169
Latest member
mm424

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