View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Ron de Bruin Ron de Bruin is offline
external usenet poster
 
Posts: 11,123
Default For Each loop leaves file open after .close

Hi Daniel

Remove the two Set sht lines and try again

--
Regards Ron de Bruin
http://www.rondebruin.nl


"Daniel" wrote in message ups.com...
Hi,

I'm running Excel 2003.

I've been trying to open & close a bunch of files, but am finding that
it leaves the file open even after I use the .close method and set the
variables = nothing. The files seems to be released upon subsequent
execution of the this line:
"For Each sht In wkbk.worksheets"

Am I not referencing something properly (or not releasing it properly)?
I tried setting the sht reference to nothing both before & after the
workbook was closed, and I tried inserting "Set sht =
wkbk.sheets("Title")" right before the "for each ..." statement, but
that didn't help either.

Here's the code:

Dim chngWkbk As Workbook

Dim sht As Worksheet
Dim wkbk As Workbook

Dim curLn As Integer

Set chngWkbk = ThisWorkbook

'work from the master file
For curLn = chngWkbk.Sheets("Sheet Setup").Range("E1").Value To
chngWkbk.Sheets("Sheet Setup").Range("F1").Value

'get the filename from the master file
If chngWkbk.Sheets("Sheet Setup").Range("A" & curLn).Value < ""
Then
Set wkbk = Application.Workbooks.Open(chngWkbk.Sheets("Sheet
Setup").Range("A" _
& curLn).Value, , 0, , , , , , , -1)

' *** files stay open until this line is executed the next time
through the loop ****'
For Each sht In wkbk.Worksheets
'do stuff
Next sht


Set sht = Nothing
wkbk.Close
Set sht = Nothing

Set wkbk = Nothing
End If
Next curLn



Thank you,

Daniel