Workbooks.Open does not activate the workbook

Felix Atagong

Active Member
Joined
Jun 27, 2003
Messages
359
I have a workbook containing macros that opens a second one (no macros) and does some 'data mining'. The following VBA is used for that (I use this code on dozen of files and it has always worked, except here):

Code:
Set WshShell = CreateObject("WScript.Shell")
ChDir (WshShell.SpecialFolders("MyDocuments"))
BladNaam = Application.GetOpenFilename("Excel File, *.xls", , "Excel")
Workbooks.Open FileName:=BladNaam
TabNaam = ActiveSheet.Name

The problem is that most of the time the newly opened workbook is NOT activated (i.e put on top) and thus the rest of the code is executed on the wrong workbook...

If I put in a Msgbox(ActiveWorkbook.Name) 9 out of 10 times the active workbook is the initial one and not the one that was opened by the code...

I tried different solutions as mentioned in the underneath thread
http://www.mrexcel.com/forum/showthread.php?t=348085
but all my attempts failed...

tried replacing Active.Workbook with wb (dim wb as workbook).
tried to wait-a-few-seconds in between code
tried renaming

The page simply refuses to get the macro's focus and it is driving me crazy :-(
 
I thank you for the observation that this it impossible, but it happens, and always with this macro on this particular sheet. It doesn't happen however when you pass line by line with the F8 command - so there is nothing wrong with the code itself... but only when the macro runs (that is why I thought the macro ran too fast for the computer and I build in some pauses, but that didn't help either)...

I have used exactly the same code on at least 60 other Excel sheets (we use it for data transfer between companies) and they all work, except... on this one... and the error arrives on whatever computer the file opens (it is not related to one single PC)...

Perhaps the Excel sheet (Beta.xls) itself has got an unrecognised error somwhere, perhaps I should just create a new one and start coding again...

Thanks for your help... if I find a solution I will post it anyway...
 
Last edited:
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You didn't answer my question. Logically, the only way it can happen is that, since your variable is declared outside your routine, another routine is changing its value. Try moving the variable declarations inside the routine and see if the problem persists.
 
Upvote 0
You didn't answer my question. Logically, the only way it can happen is that, since your variable is declared outside your routine, another routine is changing its value. Try moving the variable declarations inside the routine and see if the problem persists.

Done that finally, problem is still there.
Code:
Set wbk= Workbooks.Open(Filename:=Bladnaam)
wbk.Activate

This should activate the right workbook. But if I put as next line
Code:
Bladnaam = ActiveWorkbook.Name
Msgbox(Bladnaam)
...the box still gives the wrong workbook...
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top