Macro Command to Exit

  • Thread starter Zainuddin Zakaria
  • Start date
Z

Zainuddin Zakaria

Hi again,

This is related to the question below.
Dave Peterson has kindly responded to that and it works.
Thanks, Dave.

My next question is how to exit the workbook (file) since there is no MENU
bar again. I would like to have Macro command to do that (Exit) with a
warning message to save first.

Please help ... Thank you.

Zainuddin Z

One way is to just show the File|Save as dialog.

Option Explicit
 
C

Chip Pearson

If by "exit the workbook" you mean close the file, use something like

Workbooks("Book1.xls").Close SaveChanges:=True

No need to prompt the user to be sure to save the file. The code above will
save the file before closing, taking the decision whether to save out of the
hands of the user.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
 
Z

Zainuddin Zakaria

Thanks Chip.

Got that macro command and insert it in a module. I created a button and
assign macro to it using the macro command you gave me.

But when I clik on the macro button, I got this message "Run time error 9,
subscript out of range"

Do I have to change anything to the macro command u gave me?



Workbooks("Book1.xls").Close SaveChanges:=True



Thanks

Zainuddin Z
 
B

Bob Phillips

That is probably because you do not have a workbook called Book1.

If the code is in the same workbook as is being saved, use

ThisWorkbook.Close SaveChanges:=True


--
---
HTH

Bob

(change the xxxx to gmail if mailing direct)
 
C

Chip Pearson

The code I posted was just an example. It assumed that there would be an
open workbook named "Book1.xls". You can change "Book1.xls" to the actual
name of the workbook, or use

ActiveWorkbook.Close SaveChanges:=True

to close the workbook that is active, or

ThisWorkbook.Close SaveChanges:=True

to close the workbook that contains the code.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
 
Z

Zainuddin Zakaria

Dearest Bob Phillips and Chip Pearson,

Thank you so much. The code both of you suggested work perfectly like I
want.
I really appreciate it.

Thanks again.

Warmest regards,

Zainuddin Z
 

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