1
votes

After I run a macro(which selects records from sql server, no reference to another workbook), one of the other open workbooks is randomly activate.
The macro is in book1, I run the macro from book1.
At the end, book2(or book3 etc) is activate. Why ?!
I tried to put just before End Sub

Dim Wb As Workbook  
Set Wb = Active/ThisWorkbook  
Wb.Activate   

or

msgbox "ok"  

but still fly to another open workbook(the message box pop up on book2)
This thing not happens every time, just sometimes, randomly. Thank you

update: Since I fixed a cirrcular refference in book2, seems to stop.

1
Is the macro putting the results to a new book?Nathan_Sav
What does Debug.Print Wb.Name returns ?Stupid_Intern
Have you tried a watch on Application.Activeworkbook and breaking on change, not sure if this works, as not able to test just at mo.Nathan_Sav
I think it's because Set Wb = Activeworkbook Sets Wb variable to the workbook which is active just before running the macro. Use Set Wb = ThisWorkbook Instead.Stupid_Intern

1 Answers

1
votes

See Difference Between ActiveWorkbook and ThisWorkbook

Sub Bus()
Dim Wb As Workbook
Set Wb = ActiveWorkbook
Debug.Print Wb.Name

Set Wb = ThisWorkbook
Debug.Print Wb.Name


End Sub

If you put above code in Book2. Module 1 and Select Book1 and run it

You will get following output in the immediate Window:

Book1
Book2

enter image description here