cannot close workbook

C

chrisneill

Can somebody pse help me with this one.

I have 3 workbooks open. W(1) has the VBA code to copy
data from W(2) to W(3).

W(2) & W (3) are opened with:-

Workbooks.Open Filename:=" address "

do stuff

I then want to close W(2) and not to save but

Workbooks("W(2) Name").Activate
ActiveWorkbook.Close SaveChanges:=False .....Fails here

& keeps returning Error 9 - "Subscript out of range"
Can someone pse help me?
Thankyou.
 
J

John Green

If the workbook is saved with the file name W(2).xls use:

WorkBooks("W(2).xls").Close SaveChanges:=False

If W is an object variable array use:

W(2).Close SaveChanges:=False
 
J

John Green

Chris,

Please supply a short but complete sub procedure of sample code that illustrates your problem. The sub should show how you open the
workbook and then close it. Something like the following:

Sub Test()
Dim w(1 To 3) As Workbook

Set w(2) = Workbooks.Open(Filename:="C:\Data.xls")
w(2).Sheets(1).Range("A1").Value = 10
w(2).Close SaveChanges:=False
End Sub
 

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