VBA - Close File

  • Thread starter Thread starter dipsy
  • Start date Start date
D

dipsy

Hi! I would like to close a file without saving changes
to the file.

The file to close: "Cost.dat"
Filename is assigned to a variable -
cost = "C:\My Documents\Cost.dat"

I am using the code:
Workbooks(cost).Close savechanges:=0

I get error message:
Subscript out of range

Thanks in advance!
 
You don't supply the drive and folder to the workbooks collection.

You'll have to separate it so that it looks more like:
workbooks("cost.dat").close savechanges:=false

But another way is to use a variable to represent that workbook.

dim CostWkbk as workbook
dim Cost as string
cost = "c:\my documents\cost.dat"
workbooks.opentext filename:=cost, ....'all your code to open it
set costwkbk = activeworkbook
'do lots of stuff
costwkbk.close savechanges:=false
 
Back
Top