user2284877
user2284877

Reputation: 138

Active workbook is randomly changed after macro execution

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.

Upvotes: 1

Views: 476

Answers (1)

Stupid_Intern
Stupid_Intern

Reputation: 3460

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

Upvotes: 1

Related Questions