Suppress Worksheet_Activate When Sheet Activated From Other Macro

  • Hi. I have a userform that lets the user select an open workbook and make a few updates to the specific workbook's tabs. Some of the workbook tabs have a worksheet_activate event set and sometimes that event will run into an error (usually an unrelated subscript out of range error). I'm hoping there's a way to suppress the external book's worksheet_activate. I've tried turning enableevents off, but that doesn't seem to work as the error will still display. I also included a on error resume next in the userform's procedure, but that doesn't stop it either.


    If it helps, below is the userform code where the external workbooks sheets are looped through for certain updates.


  • Sorry, I should have specified that part. The userform will load all open workbooks in a combobox. The selected workbook becomes the wb variable in the posted code. So, no. The other workbook is already open. The idea is that many individual workbooks would be open and updated specific to each workbook could be made. Should it maybe be closed and I should have the user pick the file with the file picker to avoid the worksheet activate?

  • Your code doesn't appear to activate any sheets, and the enableevents lines will prevent any application events from firing, so I think the problem must be elsewhere. What's the code for WorksheetExists2?


    Also, just FYI:


    1. there is no need to use WorksheetFunction.Find as VBA has the InStr function.

    2. This line is pointless, since it will always be true:

    Code
    If sh.Name = "results" Or sh.Name <> "API Extract" Or sh.Name <> "Auditor Setup" Or sh.Name <> "API Write" Then


    which means that all the code from lines 16 to 27 serves no purpose.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • You were spot on with those lines not working. The intent was to bypass those sheets if they were present in the workbook. So I used the If WorksheetExists2 to see if one of those 4 were found. If so, the thought, was that it would bypass them. But, since that didn't work, I did some rearranging to remove that part and instead use if WorksheetExists2 to simply unprotect those sheets after it updates the sheet's cell protection and/or the sheet's footer. As long as those sheets aren't protected, then it should bypass further trouble there.


    So, thank you very much for that. But, back to the worksheet_activate issue, I 100% agree with you that nothing is deliberately activating these sheets. But, I think the code is still moving through those sheets to make the cell protections and footer updates, which is triggering those sheets' own code (if they have them).


    Below is the updated code, which still seems to trigger those external book's worksheet events. Thanks for your time!


  • I would use the On Error for Specialcells like this so that it only covers that error


    Code
    With sh
     .Unprotect password
     .Cells.Locked = False
     On Error Resume Next 'Added because it errors if a sheet has no formulas
     .Cells.SpecialCells(xlCellTypeFormulas).Locked = True
     On Error GoTo 0
     .Protect password
     End With
     On Error GoTo 0
     Next sh
  • Thanks royUK . That makes sense.


    Also, back to the external worksheet error. The error happens after the msgbox information telling the use if the process completed. So, after the user click okay on the msgbox, that's when the external worksheet "activates" for some reason. But, there's literally nothing after the msgbox info except Unload Me and End. So I'm at a loss for why that's happening.

  • Sorry, I've accidentally deleted my post. The suggestion was to use error handling like this so that it only works with the Specialcells error that might occur


    Code
    With sh
    .Unprotect password
    .Cells.Locked = False
    On Error Resume Next 'Added because it errors if a sheet has no formulas
    .Cells.SpecialCells(xlCellTypeFormulas).Locked = True
    On Error GoTo 0
    .Protect password
    End With
    On Error GoTo 0
    Next sh
  • Why are you using End on its own? That is not a good idea generally.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Thank you, rory . I have omitted the End as well as moved the wb.application.enableevents = True below the Unload Me. Now, there doesn't seem to be any more issues with the external sheets activating.


    Thank you for your help!

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!