Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / April 2008

Tip: Looking for answers? Try searching our database.

Close workbook

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oldjay - 15 Apr 2008 13:44 GMT
I started a post but it became too complicated. So here is my problem
I have 3 or more open worksheets
One is named Tempdata.xls and is not active.
In the active workbook I have the following macro by Alex-
Sub CloseTempData()
> Dim wkb As Workbook
>
> For Each wkb In Workbooks
> If wkb.Name = "Tempdata.xls" Then
> wkb.Close
> End If
> Next wkb
>
> End Sub

This steps thru the if - then but doesn't see and close Tempdata.xls
Joel - 15 Apr 2008 14:16 GMT
Normally when I open more than one workbook I refer to the workbook that is
running the macro with Thisworkbook.  After I open a second workbook I
automaticaly set a variable to the new book like the code below.  OPening or
adding a workbook the active workbook swithes to the new (or open) workbook.

workbooks.open filename:=abc.xls
set newbk = activeworkbook

or
workbooks.add
set newbk = activeworkbook

then close these books with
newbk.close

You problem is a temporary workbook that is not savved doesn't have the xls
extension.

> I started a post but it became too complicated. So here is my problem
> I have 3 or more open worksheets
[quoted text clipped - 12 lines]
>
> This steps thru the if - then but doesn't see and close Tempdata.xls
Dave Peterson - 15 Apr 2008 14:33 GMT
Or even:

Dim newbk as workbook
set newbk = workbooks.open(filename:="abc.xls")
or
set newbk = workbooks.add

> Normally when I open more than one workbook I refer to the workbook that is
> running the macro with Thisworkbook.  After I open a second workbook I
[quoted text clipped - 30 lines]
> >
> > This steps thru the if - then but doesn't see and close Tempdata.xls

Signature

Dave Peterson

Oldjay - 15 Apr 2008 16:08 GMT
I don't understand
All the workbooks might not have been opened at the same time.
The tempdata.xls might have been open the day before.
Where do I put this code?  

> Or even:
>
[quoted text clipped - 37 lines]
> > >
> > > This steps thru the if - then but doesn't see and close Tempdata.xls
Dave Peterson - 15 Apr 2008 16:35 GMT
How do you know which files are open and which files should be closed?

If you only go by the name, then:
workbooks("somename.xls").close savechanges:=false 'true

If you're opening the files in your code, you can use the code to keep track of
what was opened and know which one you closed.

I don't understand how you know which one is which.

> I don't understand
> All the workbooks might not have been opened at the same time.
[quoted text clipped - 46 lines]
> >
> > Dave Peterson

Signature

Dave Peterson

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.