Close workbook

O

Oldjay

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
 
J

Joel

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.
 
D

Dave Peterson

Or even:

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

Oldjay

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?
 
D

Dave Peterson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top