2

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?

1
  • To clarify, the VBA code which is triggered is the Private Sub Worksheet_Calculate() code on the first workbook. Commented Jul 25, 2019 at 22:24

2 Answers 2

4

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 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for confirming this. I'm fairly new to VBA and I was a bit surprised that the code was triggered at all. Now that I know, I'll be wary of it. I can easily fix my code to stop the current trouble. It's fairly trivial.
And thanks for the pointer to Me keyword. I wasn't aware of it and I can see how useful it is.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.