JC_RMB
JC_RMB

Reputation: 119

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?

Upvotes: 2

Views: 282

Answers (2)

LeasMaps
LeasMaps

Reputation: 302

This seems to be a bug in excel. I've posted a tentative solution here: Excel VBA Worksheet_Calculate event fires for different workbook

Upvotes: 0

Tim Williams
Tim Williams

Reputation: 166366

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

Upvotes: 4

Related Questions