Search code examples
excelvba

Open Second Workbook Triggers VBA Code in First Workbook


I open an Excel workbook which has some VBA code in it including Event code on one of the worksheets. I open a second workbook which has no VBA code in it at all but as soon as the second workbook opens, it triggers the VBA code to run in the first workbook. The code fails because the first workbook is not the Active Workbook. Anybody got any ideas why opening a second workbook should trigger event code in the first workbook?


Solution

  • I can replicate that -seems like opening a workbook triggers the calculate event in other open workbooks. Having said that, your event-handling code should not rely on any particular workbook being active when it runs, so it would help to post that if you want suggestions for fixes.

    For example the worksheet object which corresponds to the sheet module can be referenced via Me, and the workbook containing that sheet is Me.Parent

    Private Sub Worksheet_Calculate()
    
        Debug.print "calculating " & Me.Name & " in " Me.Parent.Name
    
    End Sub