Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default workbooks.open problem

Hi there

I'm having some trouble with a bit of VBA code, and its confusing me.

The code is (something like)

sThisBook = ActiveWorkbook.Name

Set wkbSource = Workbooks.Open(sPath)

If Len(sThisBook) 0 Then
'... this makes the original workbook active
Workbooks(sThisBook).Activate
End If

(I've precied it slightly)

What I find is that:

a) If I am running this code in the context of a new blank "Book1.xls" then
after the other workbook has been opened only that book is open.

i.e.

workbooks.count is 1 and workbooks(1).name is the short version of the
variable sPath

Hence

Workbooks(sThisBook).Activate

fails with an error. Since its doing

Workbooks("Book1.xls").Activate

and that is no longer open.

However,

b) If I close book1 and click New so that I have "Book2.xls" then after the
..Open statement I have 2 workbooks

and the .Activate does work since "Book2.xls" IS still open.

Is Excel implicitly closing Book1 or am I missing something?

Perplexed in England

Rob


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default workbooks.open problem

I've just run the below and it works fine! The code is written into a
book called xxxx.xls and once the code has run book xxxx.xls is
activated with PORTFOLIO being open aswell.

J

Option Explicit

Sub xxxx()

Dim sThisBook
Dim wkbSource

sThisBook = ActiveWorkbook.Name
Set wkbSource = Workbooks.Open("H:\Miscellaneous\PORTFOLIO.xls")

If Len(sThisBook) 0 Then
'... this makes the original workbook active
Workbooks(sThisBook).Activate
End If

End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default workbooks.open problem

Jase

Drat. Thanks for doing that, but now I'm even more confused.

Rob

"jase" wrote in message
ups.com...
I've just run the below and it works fine! The code is written into a
book called xxxx.xls and once the code has run book xxxx.xls is
activated with PORTFOLIO being open aswell.

J

Option Explicit

Sub xxxx()

Dim sThisBook
Dim wkbSource

sThisBook = ActiveWorkbook.Name
Set wkbSource = Workbooks.Open("H:\Miscellaneous\PORTFOLIO.xls")

If Len(sThisBook) 0 Then
'... this makes the original workbook active
Workbooks(sThisBook).Activate
End If

End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default workbooks.open problem

the name of the default workbook is not book1.xls or book2.xls. Simply
book1 or book2 without any extension.

Generally if you open a workbook with only the default workbook open (in a
pristine condition), then the default workbook is automatically closed. You
might write something to cell A1 or set Saved to False

sThisBook = ActiveWorkbook.Name
Activeworbook.Saved = False
Set wkbSource = Workbooks.Open(sPath)
workbooks(sThisBook).Activate
--
Regards,
Tom Ogilvy



"Rob Kings" wrote in message
...
Hi there

I'm having some trouble with a bit of VBA code, and its confusing me.

The code is (something like)

sThisBook = ActiveWorkbook.Name

Set wkbSource = Workbooks.Open(sPath)

If Len(sThisBook) 0 Then
'... this makes the original workbook active
Workbooks(sThisBook).Activate
End If

(I've precied it slightly)

What I find is that:

a) If I am running this code in the context of a new blank "Book1.xls"

then
after the other workbook has been opened only that book is open.

i.e.

workbooks.count is 1 and workbooks(1).name is the short version of the
variable sPath

Hence

Workbooks(sThisBook).Activate

fails with an error. Since its doing

Workbooks("Book1.xls").Activate

and that is no longer open.

However,

b) If I close book1 and click New so that I have "Book2.xls" then after

the
.Open statement I have 2 workbooks

and the .Activate does work since "Book2.xls" IS still open.

Is Excel implicitly closing Book1 or am I missing something?

Perplexed in England

Rob




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 20
Default workbooks.open problem

Tom

Once again you have supplied the answer. Allow yourself a pat on the back.

Many many thanks

Rob
"Tom Ogilvy" wrote in message
...
the name of the default workbook is not book1.xls or book2.xls. Simply
book1 or book2 without any extension.

Generally if you open a workbook with only the default workbook open (in a
pristine condition), then the default workbook is automatically closed.
You
might write something to cell A1 or set Saved to False

sThisBook = ActiveWorkbook.Name
Activeworbook.Saved = False
Set wkbSource = Workbooks.Open(sPath)
workbooks(sThisBook).Activate
--
Regards,
Tom Ogilvy



"Rob Kings" wrote in message
...
Hi there

I'm having some trouble with a bit of VBA code, and its confusing me.

The code is (something like)

sThisBook = ActiveWorkbook.Name

Set wkbSource = Workbooks.Open(sPath)

If Len(sThisBook) 0 Then
'... this makes the original workbook active
Workbooks(sThisBook).Activate
End If

(I've precied it slightly)

What I find is that:

a) If I am running this code in the context of a new blank "Book1.xls"

then
after the other workbook has been opened only that book is open.

i.e.

workbooks.count is 1 and workbooks(1).name is the short version of the
variable sPath

Hence

Workbooks(sThisBook).Activate

fails with an error. Since its doing

Workbooks("Book1.xls").Activate

and that is no longer open.

However,

b) If I close book1 and click New so that I have "Book2.xls" then after

the
.Open statement I have 2 workbooks

and the .Activate does work since "Book2.xls" IS still open.

Is Excel implicitly closing Book1 or am I missing something?

Perplexed in England

Rob








Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Macro working in new workbooks that I open chris_rip Excel Programming 2 March 29th 05 05:19 PM
Excel 2003 Workbooks.Open with CorruptLoad=xlRepairFile fails on Excel 5.0/95 file due to Chart, with Error 1004 Method 'Open' of object 'Workbooks' failed Frank Jones Excel Programming 2 June 15th 04 03:21 AM
Workbooks.Open Beeping Problem Jim McLeod Excel Programming 0 May 24th 04 11:46 PM
Excel: workbooks.open problem krunal Excel Programming 0 April 12th 04 05:44 AM
Workbooks.Open / .Open Text - How do you stop the .xls addition? Dave[_20_] Excel Programming 2 July 31st 03 04:03 AM


All times are GMT +1. The time now is 09:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"