by Allen Wyatt
(last updated June 1, 2013)
Once you start writing Excel macros, it is easy to get quite a few of them in a workbook. At some point you may want to generate a list of macros in your workbook. There is no intrinsic way within Excel to create a list of macros. You can, however, create a macro that will list your macros. (Sort of sounds redundant, doesn't it?)
As an example, consider the following macro, which steps through all the projects in your workbook to garner all the macro names and place them in a worksheet:
Sub ListMacros() Dim VBComp As VBComponent Dim VBCodeMod As CodeModule Dim oListsheet As Object Dim StartLine As Long Dim ProcName As String Dim iCount As Integer Application.ScreenUpdating = False On Error Resume Next Set oListsheet = ActiveWorkbook.Worksheets.Add iCount = 1 oListsheet.[a1] = "Macro" For Each VBComp In ThisWorkbook.VBProject.VBComponents Set VBCodeMod = ThisWorkbook.VBProject.VBComponents(VBComp.Name).CodeModule With VBCodeMod StartLine = .CountOfDeclarationLines + 1 Do Until StartLine >= .CountOfLines oListsheet.[a1].Offset(iCount, 0).Value = _ .ProcOfLine(StartLine, vbext_pk_Proc) iCount = iCount + 1 StartLine = StartLine + _ .ProcCountLines(.ProcOfLine(StartLine, _ vbext_pk_Proc), vbext_pk_Proc) Loop End With Set VBCodeMod = Nothing Next VBComp Application.ScreenUpdating = True End Sub
In order to use this macro, you must make sure you have the Microsoft VBA extensibility reference set. To do this, follow these steps:
Figure 1. The References dialog box.***Insert Figure X –
When you run the macro, it adds a new worksheet to your workbook, and then lists the names of all the macros in all the modules in the workbook.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2715) applies to Microsoft Excel 97, 2000, 2002, and 2003.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Need a list of macro shortcut keys? It's not as easy in Excel as in some other Office applications, such as Word. It can be ...Discover More
Excel allows you to assign shortcut keys to macros. Given two different workbooks each containing different macros, it is ...Discover More
Need to rename a file in a macro? It's easy to do using the Name command, as discussed in this tip.Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.