Workbook is saved on close - why?

D

Dee Veloper

Why doesn't this line of code work:
ActiveWorkbook.Close savechanges = False

nor does this one:
Databook.Close savechanges = False
in both cases above the file gets saved even though it shouldn't

but this one works
DataBook.Close False
all variables are declared


code excerpt:
For i = 1 To 10
Workbooks.Open .Files(i)
Set DataBook = ActiveWorkbook
For Each ws In ActiveWorkbook.Worksheets
ws.Activate
MonthStamp
Range("data").Copy
NewWkBook.Activate
ActiveSheet.Paste
Next
DataBook.Activate
Application.DisplayAlerts = False
DataBook.Close False 'this works
'ActiveWorkbook.Close savechanges = False doesn't work
NewWkBook.Activate
Next i
 
C

CJ

Have you tried this code instead?

ActiveWorkbook.Close SaveChanges:=False

I think you need the colon in there.
 
C

Chip Pearson

There are two problems at work here.

ActiveWorkbook.Close savechanges = False

First, you don't have Option Explicit in your module. Therefore, VBA will
declare a variable when it encounters a variable name and give it a default
value. The second problem is that you are missing the colon in the named
argument assignment. Without the colon, VBA sees 'savechanges' as a
variable name, not a named argument, so it creates the variable and gives it
a default value of False (since it is used to compare against the boolean
value False). Therefore, your code is the same as

ActiveWorkbook.Close (False = False) doesn't work

And since 'False = False' evaluates to True, you are passing a value of True
to the first argument of Close, which causes the workbook to be saved.

All that said, you need to include the colon in the named argument
assignment:

ActiveWorkbook.Close savechanges:= False


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 

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