Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Counting the Times a Worksheet is Used.

Counting the Times a Worksheet is Used

Written by Allen Wyatt (last updated May 30, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


2

You may want a way to keep track of how many times a particular worksheet is used. There are many ways you can accomplish this. One simple way is to just store the count in the worksheet itself. Right-click a worksheet tab, then choose View Code from the Context menu. Excel displays the Visual Basic Editor, where you should paste the following code:

Private Sub Worksheet_Activate()
    Range("A1").Select 'customize Range
    ActiveCell = ActiveCell + 1
    Range("B1") = "times opened" 'customize Range
End Sub

This code increments the value in cell A1 every time the worksheet is activated. You can modify the cell locations where the macro writes its information, according to your needs.

A more thorough approach is to create a macro that increments named references within the workbook. Consider the following macro:

Function IncrementEventCounter(sName As String, sht As Object)
    On Error Resume Next
    If sht.Names(sName) Is Nothing Then _
      ThisWorkbook.Names.Add "'" & sht.Name & "'!" & sName, "1", False
    On Error GoTo 0
    With ThisWorkbook.Names("'" & sht.Name & "'!" & sName)
        .RefersTo = Val(Mid(.Value, 2)) + 1
    End With
End Function

This function is designed to be called from a different macro—one triggered by the event that should cause the usage counter to increment. For instance, if you want to keep track of every time the worksheet is activated, then you would use the following macro as part of the ThisWorkbook object:

Private Sub Workbook_SheetActivate(ByVal sh As Object)
    IncrementEventCounter "Activated", sh
End Sub

The macro increments a counter named "Activated" for the worksheet. It does this by calling the IncrementEventCounter macro, with the name of the counter and the name of the worksheet. If, instead, you wanted to count the number of times that a worksheet was changed, you could use the following macro as part of the ThisWorkbook object:

Private Sub Workbook_SheetChange(ByVal sh As Object, _
  ByVal Target As Excel.Range)
    IncrementEventCounter "Changed", sh
End Sub

The only difference between this macro and the previous one is that it increments a counter named "Changed." To see the values of the counters, just enter a formula in a cell that references the counter. For instance, you could enter =Changed to see the value of the Changed counter, or =Activated to see the value of the Activated counter. The value of each counter will differ from sheet to sheet, since the counters are maintained on a sheet-by-sheet basis.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10544) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Counting the Times a Worksheet is Used.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Repeating Rows for a Table Footer

Word allows you to specify rows that should be repeated at the top of a table when that table extends beyond the bottom ...

Discover More

Batch Template Changes

Changing the template associated with a couple of documents is easy, but what if a whole directory needs to be changed? ...

Discover More

Viewing Two Worksheets At Once

If you need to work on two worksheets in the same workbook at the same time, Excel makes this rather easy to do. All you ...

Discover More

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!

More ExcelTips (ribbon)

Item Not Available in Library

When sharing workbooks with others, you may find that the macros in those workbooks may not work as you expect. This tip ...

Discover More

Error Creating Event Handlers

If you are getting an error when you try to create an event handler, it could be related to a long-known bug in Excel. ...

Discover More

Clean Up Your Macro List

Got a workbook cluttered with all sorts of macros? Delete them and you'll make your workbook easier to manage.

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is seven minus 2?

2020-08-21 10:15:23

J. Woolley

@Brendan
Yours would be a good question for wellsr VBA Q&A: https://ask.wellsr.com/vba
Be sure to cross-reference this Tip's web address.


2020-08-20 16:12:49

Brendan

At work we have a excel template that we use to print different labels for equipment. At the moment we use another spreadsheet to track how many of each piece of equipment we print labels for.
Would it be possible that each time we hit print on the label we can have another sheet in excel that would total up the different types Of equipment for so I can present this into graphs ?
Many thanks


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.