Microsoft Excel Workbook Metrics

A small VBA code snippet to display the most important metrics of an Excel workbook like the count of sheets, cells, formulas, constants, named ranges, charts, tables, lines of code and many more

Microsoft Excel Workbook Metrics IntroWhen you are developing a large and complex Microsoft Excel model, you should definitely have an eye on the size of your workbook and its components: how many sheets does your workbook have? How many used cells? How many formula and constant cells? How many of the formulas return an error value? How many Named Ranges are defined? How many Charts, Tables, Pivot Tables, lines of VBA code, etc.?

These metrics will give you an overview on the size and complexity of your model, point you to areas with improvement potential and help you to optimize performance and minimize file size.

Some Microsoft Excel productivity Add-Ins have features providing this information. But what if you do not have such an Add-In available? Or what if you want to share your workbook with your colleagues or deliver it to a client? In this case it is very helpful to have a VBA code snippet which automatically calculates at least the most important workbook metrics and can easily be included in any workbook without the need of installing an Add-In.

Today’s article provides this code snippet to detect and display the most important key figures of a Microsoft Excel workbook. As always, the post comes with an example workbook including the open VBA code for free download.

The Excel Workbook Metrics

The following workbook metrics are detected and displayed in a message box:

  • Model Size: physical size of the workbook in Megabytes and Bytes
  • Worksheets: count of worksheets in the workbook (total, visible, hidden and very hidden).
    In case you are not familiar with the .Visible property of a worksheet: very hidden sheets are worksheets which will not appear in the Unhide Sheet dialogue window of Excel. You can only change the visibility by setting the .Visible property in the VBE from xlSheetVeryHidden to xlSheetVisible or xlSheetHidden
  • Used Cells: count of used cells in the workbook, i.e. across all sheets (total, visible and invisible cells)
  • Blank Cells: count of blank (empty) cells
  • Constant Cells: count of cells containing a constant (number or text)
  • Formula Cells: count of cells containing a formula
    The metrics dialogue window does not only show the total number of formulas, but also divided by formulas returning numbers, texts, logical values (true or false) or errors. The number of error formulas contains all cells returning an error value like #N/A, #REF! or #VALUE!. You should definitely have an eye on the count of error formulas, but you also have to keep in mind that you sometimes may have written formulas which return an error value on purpose (e.g. producing #N/A for data source series which shall not be plotted). In a nutshell: if the count of error formulas is greater than 0, this does not necessarily mean that you have broken formulas or reference errors in your workbook.
  • Cond. Formatted Cells: count of cells with a conditional format
  • Data Validation Cells: count of cells with a data validation rule
  • Named ranges: count of named ranges
    Please note that this metric does not include the names of the tables / list objects (although table names are shown in the Name Manager).
  • Pivot Tables: count of Pivot Tables
  • Charts: count of charts (worksheet embedded charts only, i.e. without chart sheets)
  • Other Objects: count of other objects like freeform shapes or images
  • External links: count of links to other Excel workbooks
    The count of external links does not represent the number of cells containing a link to another workbook, but rather the count of other Excel workbooks your model is referring to. I.e. if you have 500 cells referring to the same external workbook, the count of external links will still be only one.
  • VBA components: count of VBA modules, class modules and worksheet objects which actually contain VBA code
  • Lines of VBA code: count of lines of VBA code across all VBA modules
    This metric also includes all comment and blank lines in the code.

There are other key figures you may be interested in, like e.g. the count of chart sheets or the count of OLE and DDE links. I only included the metrics I am most interested in. If you want to add something, the code will most likely be along the same lines as for the other metrics and it shouldn’t be too difficult to add the necessary statements.

The VBA Implementation

The VBA implementation is one main sub (WorkbookStatistics) and three small helper functions (CountVBComponents, LinesofCode and CountofTables). Most of the metrics are detected by looping through all sheets of the active workbook and taking advantage of the .Cells and .SpecialCells properties of the Range object.

The code is pretty straight forward, so I won’t go into the details here. If you are interested, please download the example workbook (download link see below) and have a look.

Finally, instead of displaying the results in a message box, you could also write the results to a dedicated range on a predefined or newly inserted worksheet. No big deal, all you have to do is to replace the message box statement by a series of VBA lines writing the defined variables to a cell range on a sheet.

Examples

Here are a couple of examples demonstrating the Excel workbook metrics procedure in action:

A screenshot of the example workbook posted for download below:

Microsoft Excel Workbook Metrics Example 1 - click to enlargePlease be advised that the calculations, charts, named ranges, etc. in this workbook do not make much sense. The only purpose of this workbook is to provide the code and to demonstrate the results.

Here are the metrics of the workbook which came with the recent article Highlight Actions in Microsoft Excel:

Microsoft Excel Workbook Metrics Example 2 - click to enlargeThere is a little bit more going on here than in the example workbook above, but it is still a relatively small Excel model.

Finally, here are the metrics of a larger Strategic Financial Model I recently developed for one of my clients:

Microsoft Excel Workbook Metrics Example 3 - click to enlarge

24 worksheets, more than 700,000 used cells, almost 300,000 formula cells, 14 named ranges, 5 tables, 17 charts, 820 lines of code. This should make clear why I like to keep an eye on the workbook metrics of my models.

How to transfer the VBA code to your own workbook

Here is a step-by-step how to transfer the VBA sub to your own workbook:

  • Download the example workbook (download link see next section)
  • Unzip the downloaded file and open the workbook
  • Open your own workbook
  • Go to the VBE (Alt-F11)
  • In the Project Explorer of the VBE (press CTRL-R, if the Project Explorer isn’t visible), click on the module “modWorkbookMetrics” and drag it to the VBE project of your workbook
  • Finally, close the example file and save your workbook as a macro-enabled Excel workbook
  • The VBA sub WorkbookStatistics is now available in your model and you can run the code from the View Macro dialog window (ALT-F8) or you assign the sub to a command button or an icon on a sheet

Download Link

Here is the Metrics Example workbook for free download:

Download Workbook Metrics Example (zipped Excel 2007-2016 workbook, 59K)

Stay tuned.

Comments

22 responses to “Microsoft Excel Workbook Metrics”

  1. Frank Avatar
    Frank

    Hi, Looks like great information. I’ve downloaded your file, activated macros in Excel and run the WorkbookStatistics macro. Unfortunatelly, nothing is happening. I’m running Excel 2013. Any idea?

  2. Robert Avatar

    Frank,
    I developed and tested the workbook with Excel 2016 only, but I do not see why it should not work with 2013 or 2010. Go to the VBE, comment out or delete the line
    On Error Resume Next
    and try to run the macro again. If an error message is displayed, click on debug and let me know at which line the code stopped.

  3. Robert Avatar

    Frank,
    Roberto Mensa just provided me with an idea what your issue may be (many thanks, Roberto):
    Open Excel and go to File | Options | Trust Center | Trust Center Settings | Macro Settings. In this window activate the check box next to “Trust access to the VBA project object model”. Then try again to run the macro.
    Does this solve your issue?

  4. Frank Avatar
    Frank

    Many thanks Robert. The 2nd hint was the solution. Never seen this setting before. The Macro gives a good overview, will definitelly use it in my models.
    One remark: worksheets need to be unprotected, otherwise no information can be obtained!

  5. roberto mensa Avatar

    Hi Robert,
    always in your articles we learn … great tips and tricks and many interesting ideas
    Thank you for sharing with us
    see you soon

  6. Robert Avatar

    Frank,
    yes and no.
    You are right, some statistics are not detected correctly if worksheets are protected, but only the metrics which are calculated using the .SpecialCells property, i.e. blank, constant, formula cells and the cells with conditional format and data validation. All other metrics are including the protected worksheets, too.
    If you know for sure the sheets are not protected with a password, you would add the following lines of code to include the protected sheets:
    Define a Boolean variable to store the current protection status:
    Dim blnProtected As Boolean
    Inside the loop through all worksheets, add these lines at the beginning of the code:
    blnProtected = .Protection
    If blnProtected Then .Unprotect
    and this one at the end:
    If blnProtected Then .Protect
    This should do the job.
    Although, as already mentioned above, this will not work with password protected sheets.

  7. Josh Avatar
    Josh

    Hello. “Trust access to the VBA project object model” was what allowed me to run the macro as well. Thanks for the tip in the comments.

  8. Josh Avatar
    Josh

    Forgot to mention: I’m running Excel 2016.

  9. Mike Avatar
    Mike

    Hi Robert,
    I’m running Office Professional Plus 2016. When I try to run your macro on one of my workbooks, I keep getting an “Ambiguous name detected: Format” error in the Messagebox section of code. Location: “Call MsgBox(“Model Size: ” & vbTab & Format(dblfileSize, C_NUMBER_FORMAT_1) & ”
    It’s the first Format before (dblfilesize, …
    Any idea why it would be doing this? Thanks in advance.

  10. Robert Avatar

    Mike,
    I would assume you have a sub, a function, variable or a constant defined somewhere in your own code with the name “Format”. This declaration is in conflict with the name of the VBA function “Format” I am using in the Message Box. Simply rename the sub or variable you named “Format” and it should work.

  11. Hans Knudsen Avatar
    Hans Knudsen

    Hi Robert,
    I have activated the check box next to “Trust access to the VBA project object model”, but when I run the WorkbookStatistics macro the only thing that happens is that I get a Run time error 1004 (No cells were found) and the following code line is highlighted in yellow:
    Set rngCheck = .UsedRange.SpecialCells(xlCellTypeFormulas, xlTextValues)
    I have deleted the line:
    On Error Resume Next

  12. Robert Avatar

    Hans,
    “On Error Resume Next” was there on purpose. If .SpecialCells doesn’t find anything (in your example formula cells returning a text), the code will throw the error. On Error Resume Next simply skips the line and the code will finish without the error.
    Simply put back in “On Error Resume Next” and you should be ok.

  13. Hans Knudsen Avatar
    Hans Knudsen

    Thank you very much Robert. Now everything is fine.

  14. Shobi Imran Avatar

    Very informative article, great work
    thanks for sharing, Keep up the good work!

  15. TML Avatar
    TML

    Hi Robert,
    Thank you for your contribution. Amazing work as always.
    When running this code, the formatting on my numbers is inconsistent…some numbers are right aligned and others are left aligned. I did not do anything to change your code. Do you know what may be causing this?
    Thank you

  16. Robert Avatar

    TML,
    many thanks for your comment and your kind words. Much appreciated. Actually, I have noticed this problem, too, but did not mention it in the article. My bad, I am sorry.
    The alignment of the numbers in the message box is done by inserting tabs (vbTab) in the strings used for the message box. Unfortunately, this does not work properly for all screen resolutions. Truth be told, I have no idea how to fix this issue. All you can do is inserting additional or deleting existing vbTabs in the MsgBox statement at the end of the code by trial and error until the text in the Message Box looks good on your machine.
    As I said, I do not know how to make this work for all screen resolutions. I am sorry.

  17. David Sanchez Avatar
    David Sanchez

    Hi Robert
    Thank you for your article.
    When I run the macro it only shows the model size but nothing else.
    I enabled the trustcenter settings.
    Dough it shows the message box, it is still working in the background.
    Have you an idea why it only shows this?

  18. Robert Avatar

    David,
    the code cannot detect the count of constant and formula cells, the conditionally formatted cells, the cells with data validation, etc. as long as the worksheets are protected. However, it should show the count of used cells, the count of tables, Pivot tables, charts, lines of VBA code, etc. even if the sheets are protected.
    Are the sheets protected in your workbook? If so, unprotect them and try again.
    If this does not solve your problem, you can send me your workbook by email (email-link see the top of the blog) and I will have a look.

  19. David Sanchez Avatar
    David Sanchez

    Thank you for your reply.
    It seems very strange, I used your samplebook and disabled everything that could potentially cause a protection I even unblocked the file on properties.
    I will send you an email with the exact download I used

  20. Lukas Avatar

    I linked back to your great Tool today! Thanks for providing it.

  21. Azeem Avatar
    Azeem

    Hi Robert,
    I am a typical data average excel user to clean data by mannual excel tools. Now I have been given a huge task of cleaning multiple sheets. I have tried using ur metrics but I cant move forward after taking few baby steps. I need help please.
    Your Last step#8 “The VBA sub WorkbookStatistics is now available — dialog window (ALT-F8) ” is not working for me
    I will send you screen shots in email.
    Regards

  22. Robert Avatar

    Azeem,
    I have not (yet) received your email and based on the information provided in your comment, it is hard to tell what the root cause of your problem might be.
    However, here is one thing you might want to check out: if the short cut ALT-F8 does not bring up the macro window, you may also try clicking on the icon Macros on the Design Tab instead. Maybe the short cut ALT-F8 is occupied by another application on your system.

Leave a Reply

Your email address will not be published. Required fields are marked *