Properties for Worksheets

Written by Allen Wyatt (last updated December 14, 2024)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

Laurie knows how to view properties of an Excel workbook. What she would like to do, however, is to view similar properties relative to individual worksheets. For instance, she wonders if there is a way to view properties such as date created, date modified, author, or "last modified by" for individual worksheets.

Unfortunately, Excel doesn't keep track of such information for worksheets. The only workaround we've been able to figure out is to develop your own record of information about the worksheets in the workbook. An obvious way to do develop a Workbook_SheetChange event handler. The following is an example of one you could add to the ThisWorkbook object in the Visual Basic Editor:

Private Sub Workbook_SheetChange(ByVal ws As Object, ByVal Target As Range)
    Dim s As Worksheet
    Dim J As Integer
    Dim FoundIt As Boolean

    On Error Resume Next
    Set s = Worksheets("Stats")
    On Error GoTo 0
    Application.EnableEvents = False
    If s Is Nothing Then
        ' Stats worksheet did not exist
        Set s = Worksheets.Add(After:=Worksheets(Worksheets.Count))
        s.Name = "Stats"
        s.Range("A1") = "Worksheet"
        s.Range("B1") = "Creator"
        s.Range("C1") = "Last Modified"
        s.Range("D1") = "Modifed By"
        
        With s.Range("A1:D1")
            .Font.Bold = True
            .Borders(xlEdgeBottom).LineStyle = xlContinuous
            .Borders(xlEdgeBottom).Weight = xlThin
        End With

        s.Range("A2") = s.Name
        s.Range("B2") = s.CustomProperties.Creator
        s.Range("C2") = Format(Now, " mm/dd/yyyy  hh:mm am/pm")
        s.Range("D2") = Application.UserName
    End If
    J = 2
    FoundIt = False
    While (s.Cells(J, 1) <> "")
        If s.Cells(J, 1) = ws.Name Then
            FoundIt = True
            s.Cells(J, 3) = Format(Now, " mm/dd/yyyy  hh:mm am/pm")
            s.Cells(J, 4) = Application.UserName
        End If
        J = J + 1
    Wend
    If Not FoundIt Then
        ' Worksheet name not found
        s.Cells(J, 1) = ws.Name
        s.Cells(J, 2) = ws.CustomProperties.Creator
        s.Cells(J, 3) = Format(Now, " mm/dd/yyyy  hh:mm am/pm")
        s.Cells(J, 4) = Application.UserName
    End If
    ws.Activate
    Application.EnableEvents = True
End Sub

The event handler is triggered anytime you make a change in the workbook. It first checks to see if there is a worksheet named Stats. If not, then the worksheet is created, and some rudimentary information is added to it. The handler looks in the Stats worksheet to determine if the data there includes a row for the worksheet on which a change occurred. If not, then a row is added, but if so, the information in the row is updated.

The handler only tracks four pieces of information—the worksheet name, the creator, the date the last change was made, and the user name of who made the change. (The Creator property indicates a numeric value related to the program that created the worksheet. It isn't terribly helpful for humans, and I included it as an illustrative example of how information can be stored.)

Remember, this is only a workaround and you should consider carefully what type of information you want to track for your worksheets. You can then modify the code to reflect that desire.

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 (7542) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021.

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

Adding Hyphens to Phrases

Editing text to turn regular words into hyphenated phrases can be a real bother. The chore can become a breeze if you ...

Discover More

Determining the Current Directory

When creating macros, it is often necessary to know which directory is the default. Here's how you can find out by using ...

Discover More

Deleting Old Data from a Worksheet

If you keep on-going data in a worksheet, some of your data�"over time�"may need to be deleted. If you have an ...

Discover More

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!

More ExcelTips (ribbon)

Condensing Multiple Worksheets Into One

Want a quick way to combine your worksheets? This tip features a simple macro to do the task for you.

Discover More

Quickly Copying Worksheets

Excel provides a little-known way to copy worksheets simply by clicking and dragging. Here's how to do it.

Discover More

Finding a Worksheet with a Specific Value in a Specific Cell

If you have a lot of worksheets in workbook, finding the exact one you want can be a bit tricky. This tip looks at ...

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 eight less than 8?

2024-12-16 11:16:19

J. Woolley

The Tip's event handler clears the Undo stack each time a change is made to any sheet in the workbook; therefore, it is impossible to Undo (Ctrl+Z) any change.
On the other hand, the procedures described in my previous comment below preserve (do NOT clear) the Undo and Redo stacks; Ctrl+Z will Undo any change.
For more on this subject, see my recent comment here:
https://excelribbon.tips.net/T012513#comment-form-hd


2024-12-15 15:31:07

J. Woolley

The Tip's macro refers to ws.CustomProperties.Creator, which is the same as ws.Creator and Application.Creator. The Excel value is 1480803660 (&H5843454C); those 8 hexadecimal values represent the 4 ASCII characters XCEL.
Each worksheet can have its own custom properties. The following ThisWorkbook event handlers store created and modified date, time, and user information for each worksheet as custom property text:

Private Sub Workbook_NewSheet(ByVal Sh As Object)
    If TypeOf Sh Is Worksheet Then
        Sh.CustomProperties.Add Name:="Created", Value:=CPValue()
    End If
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim oCP As CustomProperty
    If TypeOf Sh Is Worksheet Then
        For Each oCP In Sh.CustomProperties
            If oCP.Name = "Modified" Then oCP.Delete: Exit For
        Next oCP
        Sh.CustomProperties.Add Name:="Modified", Value:=CPValue()
    End If
End Sub

Private Function CPValue() As String
    CPValue = FormatDateTime(Date, vbShortDate) _
        & " " & FormatDateTime(Time, vbLongTime) _
        & " by " & Application.UserName
End Function

Notice "Created" information is only available for worksheets added after implementing the NewSheet event handler. "Modified" information is updated after any worksheet is changed. Each worksheet's custom properties are retained when the workbook is saved.
The following macro will report "Created" and "Modified" information for the active worksheet:

Sub CreatedModified()
    Dim oCP As CustomProperty, sCreated As String, sModified As String
    If TypeOf ActiveSheet Is Worksheet Then
        sCreated = "(unknown)"
        sModified = sCreated
        For Each oCP In ActiveSheet.CustomProperties
            Select Case oCP.Name
            Case "Created": sCreated = oCP.Value
            Case "Modified": sModified = oCP.Value
            End Select
        Next oCP
        MsgBox "This worksheet was created" & vbLf & sCreated & vbLf _
            & vbLf & "This worksheet was modified" & vbLf & sModified
    Else
        MsgBox "The active sheet is not a worksheet"
    End If
End Sub

My Excel Toolbox includes the following function to return the value of a custom worksheet property:
    =GetCustomWSProperty(Name, [Sheet])
Name's case is ignored if custom worksheet property Name exists; otherwise, #VALUE! error is returned.
If optional Sheet is omitted, the custom property applies to the formula cell's worksheet; otherwise, it applies to the specified Sheet (case ignored) in that cell's workbook. Therefore, these formulas will return "Created" and "Modified" information for the active worksheet:
    =GetCustomWSProperty("Created")
    =GetCustomWSProperty("Modified")
Press F9 to update those results.
The following dynamic array function lists any custom worksheet properties:
    =ListCustomWSProperties([AllSheets], [SkipHeader])
If AllSheets is FALSE (default), only the formula cell's worksheet will be evaluated; otherwise, all worksheets in the workbook are included. Therefore, the following formula provides an alternative to the Tip's "Stats" worksheet:
    =ListCustomWSProperties(TRUE)
Again, press F9 for the latest results.
The SpillArray function (described in UseSpillArray.pdf) simulates a dynamic array in older versions of Excel.
See https://sites.google.com/view/MyExcelToolbox


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.