Workbooks.Open does not activate that workbook

Felix Atagong

Active Member
Joined
Jun 27, 2003
Messages
359
I have a workbook (Me.xls) that opens a second one and does some 'data mining'.
The following VBA is used for that:

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 in some rare cases the newly opened workbook is NOT activated (i.e put on top) and thus the rest of the code is executed on the wrong active sheet from the wrong active workbook...

Code:
Cells.Replace What:=Chr(34), Replacement:=" ", LookAt:=xlPart, SearchOrder _
        :=xlByRows, MatchCase:=False
Sheets.Add
ActiveSheet.Name = "xyzabc"
Sheets(TabNaam).Activate
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:="MeTemp.xls", FileFormat:=xlNormal, _
        Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
        CreateBackup:=True
Application.DisplayAlerts = True
' LOTS OF CODE AFTER THAT

It is apparently not the PC that acts too slow because the same page gives the same error on different machines with different speeds (Excel 2000).

I put in a Msgbox(ActiveWorkbook.Name) and indeed in 9 out of 10 times the active workbook is the one that was first opened and not the second...

Is there a way around that?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Instead of using activeworkbook use the acual workbook needing to be used:

Code:
dim wb as workbook
set wb = Workbooks.Open(FileName:=BladNaam)

Then anywhere you have activeworkobook substitue wb. for it.

Hope that helps.
 
Upvote 0
Tried that already, doesn't work. 8-( Could it be that the pagename is too long (and it has several spaces between words)???

I put in a counter that holds the macro for 3 seconds, you clearly see (on screen) how the second page is opened and how it opens on top of the first page. So you might think it has been activated.

The problem is, it is not, the macro still runs on the underlying page.
 
Upvote 0
Found it.

Apparently some of the pages that were opened already contained a sheet with a "xyzabc" sheet inside.

The strange thing is that Excel didn't give the error that I tried to rename a new sheet with an already existing name but that it carried on as if nothing had happened... (but crashing a while later)...
 
Upvote 0
Please disregard my previous post as that isn't the issue with all problems.
Apparently the macro runs too fast for Excel to react with that page.
The page isn't entirely open when the macro already executes the next command.
Putting a 3 seconds wait function after opening the file did the trick.
 
Upvote 0

Forum statistics

Threads
1,215,401
Messages
6,124,705
Members
449,182
Latest member
mrlanc20

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