Richard's company, like many others, uses Excel quite a bit. In fact, they have thousands and thousands of Excel workbooks that they have collected over the years. Richard needs a way to find out which of those workbooks have VBA macros in them, without the need to open and inspect each workbook individually. He wonders if there is an easy way to do this.
One rather simplistic way to find all your workbooks containing macros is to just look for any files that use the XLSM or XLSB extensions. Workbooks that contain macros must be stored in files using these extensions. While not 100% foolproof, it is a good place to start.
You could also use the search capabilities of Windows (outside of Excel) and search for any file that contains the text "End Sub" or "End Function". That will quickly identify any potential candidate workbooks, as any VBA procedure must use one of these two statements at its end.
If you are using legacy workbooks (those developed using Excel 2003's file format), then you actually need to look inside each of the workbooks. This can be done programmatically, meaning that you could have a macro that opens each workbook in a folder and examines it to see if there are any macros within it.
As an example, you could create a macro that steps through each of the files in a directory and determines if the file is an Excel workbook. It can then open the file and check to see if it has a VBA project within it.
Sub FindMacros() Dim sPath As String Dim sFile As String Dim sFoundFiles As String 'specify directory to use - must end in "\" sPath = "C:\MyData\Excel Data\" sFile = Dir(sPath) Do While sFile <> "" If InStr(sFile, ".xls") > 0 Then Workbooks.Open (sPath & sFile) If Workbooks(sFile).HasVBProject Then sFoundFiles = sFoundFiles & sFile & vbCrLf End If Workbooks(sFile).Close (False) End If sFile = Dir ' Get next filename Loop If Len(sFoundFiles) = 0 Then MsgBox "No workbooks found that contain macros" Else sFoundFiles = "The following workbooks contain macros:" & _ vbCrLf & vbCrLf & sFoundFiles MsgBox sFoundFiles End If End Sub
This example uses the HasVBProject property (introduced to the Excel object model in Excel 2007) to determine whether the file has any macros or not. When complete, the macro displays a message box that lists those worksheets containing macros.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12466) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Finding Workbooks Containing Macros.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
VBA makes it easy to copy a worksheet from the current workbook into a brand-new workbook. You may want to delete some ...
Discover MoreDo you often need to know the difference between two values in your worksheet? This tip shares a quick little macro that ...
Discover MoreDo you have a macro that needs to read and write files? If so, then there is a good chance you need to specify the ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-11-27 04:10:46
Hans Hallebeek
I use this UDF to make sure that the filepath always has a final trailing backslash
The parameter passed is the filepath
Public Function TrailingSlash(varIn As Variant) As String
If Len(varIn) > 0 Then
If Right(varIn, 1) = Application.PathSeparator Then
TrailingSlash = varIn
Else
TrailingSlash = varIn & Application.PathSeparator
End If
End If
End Function
2020-11-26 05:47:27
achille
exactly what i m looking for
i tested it for doc file i wrote a similar macro inside a doc file to make the same thing
it seems not workning properly for word file
the method HasProject () does not differ properly if a word file contains a macro
so my question there is a similar method as hasproject() for doc file???
also there is a similar method for PPtx file??
2020-08-12 05:01:56
Willy Vanhaelen
If you look into a XLSB file to see if it contains the text "End Sub" or "End Function" you will always get a negative result even if the file has macos in it, because the vbaProject.bin is a binary file.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2022 Sharon Parq Associates, Inc.
Comments