+ Reply to Thread
Results 1 to 6 of 6

Capture Add-Delete Worksheet Events

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Capture Add-Delete Worksheet Events

    I would like some help capturing, and incorporating, both the “Add Sheet” and “Delete Sheet” events for a workbook.

    I have a Dynamic Range on a sheet in which this Workbook’s sheets names, minus any I don’t want listed, are placed by a macro.

    What I need help with, is if the User Adds a sheet or Deletes a sheet, the change will trigger the macro I have in place.

    The Macro called “UpdateSheets” works as follows:
    1. It first clears the “SHEETS” dynamic range on the “INGREDIENTS” sheet.
    2. The sheet names are then relisted, taking into account any that have been added or deleted.
    3. The list is then sorted.

    Sub UpdateSheets()
      Dim Sh As Worksheet
    
        'Clear Existing List from SHEETS range
            Call ClearSheetRange
    
            Application.ScreenUpdating = False
    
        'Loop through all worksheets except for TEMPLATE and INGREDIENTS worksheets
            For Each Sh In ActiveWorkbook.Worksheets 
            If IsError(Application.Match(Sh.Name, Array("TEMPLATE", "INGREDIENTS"), 0)) Then
    
        'Print the Sheet names to column "E"
            Sheets("INGREDIENTS").Range("D" & Rows.Count).End(xlUp).Offset(1, 0) = Sh.Name
            End If
    
                Next Sh
    
        ' Run sorting macro
            Call SortSheetsList
    
            Application.ScreenUpdating = True
    
    End Sub
    I’ve seen one example in which a Delete Class module was written, but I was unsure how to adapt that code into something I could use here.

    One thing to remember is that the INGREDIENTS sheet will not be the active sheet when this event is triggered, so I wish to avoid a “Runtime Error 1004” Method of Range Object_Global Failed.
    (I have rotten luck fixing these.)

    There is a workbook attached for illustration purposes.

    Thanks for your time.
    Attached Files Attached Files
    Last edited by max57; 03-15-2010 at 01:34 PM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Capture Add-Delete Worksheet Events

    Hi,

    There's a workbook 'NewSheet' event which you could use to call the UpdateSheets macro, but no similar DeleteSheet event, so perhaps it may be better to use the Workbook BeforeSave event and call the macro from there.

    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Capture Add-Delete Worksheet Events

    Thanks Richard.

    When a new sheet is added, it does the trick, except for that Error 1004.
    I can't seem to get the reference to the sheet correct.
    I've tried This workbook and Sheets and more. No luck.

    Sub ClearSheetRange()
    
      'It bugs out here - Error 1004, Range Method.
        Range("SHEETS").ClearContents
    
    End Sub
    Thanks,
    Mark

  4. #4
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Re: Capture Add-Delete Worksheet Events

    I've fixed the Error and have gotten the "ADD SHEET" portion of the code to work.

    I tired using Workbook_BeforeSave but I wasn't sure how to code it.
    I then tried Workbook_Deactivate, but it kept deleting the "INGREDIENTS" sheet.

    I'm at a loss on how to work around this.
    Any ideas?

    There is an updated version of the file attached
    .
    Attached Files Attached Files

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Capture Add-Delete Worksheet Events

    Deleting / Inserting a sheet should invoke the Sheet Activate event.

    In theory you could on that basis use that Workbook level event to store a Static variable keeping track of the number of sheets in that workbook.
    As and when the number changes invoke the other routine.

    (the Static will be lost between "sessions" so the code will also fire the first time the workbook calculates)

    Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Static lngWs As Long
    On Error Resume Next
    If ThisWorkbook.Sheets.Count <> lngWs Then
        'new sheet or sheet removed
        'disable events
        Application.EnableEvents = False
        'do stuff....
        'enable events
        Application.EnableEvents = True
        'update Static
        lngWs = ThisWorkbook.Worksheets.Count
    End If
    End Sub
    the above would reside in ThisWorkbook in VBE

    (the above would need to usurp the NewSheet event - not ideal but as outlined in should cater for both insertion & deletion simultaneously)
    Last edited by DonkeyOte; 03-15-2010 at 08:59 AM. Reason: added comments

  6. #6
    Forum Contributor
    Join Date
    07-11-2009
    Location
    NYC,USA
    MS-Off Ver
    Excel 2007
    Posts
    135

    Thumbs up Re: Capture Add-Delete Worksheet Events

    DonKeyOte,

    That was perfect!
    Static Variables. Another useful piece of information for me to incorporate.
    Much obliged for the help.

    Mark

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1