Save Workbook VBA

JohnZ1156

Board Regular
Joined
Apr 10, 2021
Messages
180
Office Version
  1. 2021
Platform
  1. Windows
I have a macro to print out a page. Then I have this at the very end of my macro.
But after I get the message box that the Workbook has been saved, and I press "OK". when I go to close the workbook by clicking on the red "X" in the top right hand corner, it asks me if I want to save the Workbook. Why?

VBA Code:
    MsgBox ("The " & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " Workbook was saved!")
    ThisWorkbook.Save

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hello!

In a message box you talk about ActiveWorkbook, but the macro saves ThisWorkbook. ThisWorkbook is always a reference to the book in which the code is written. ActiveWorkbook - open and activated book at the moment of macro execution. So, try
VBA Code:
ActiveWorkbook.Save
MsgBox ("The " & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " Workbook was saved!")
or
VBA Code:
ActiveWorkbook.Close 1
MsgBox ("The " & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " Workbook was saved!")
to close your book without mouse clicking.
 
Upvote 0
Hi LazyBug

Thanks for your help.
After the macro prints and saves, I'd rather NOT automatically close the WorkBook. Just in case I want to look at something else.

But, even after changing to your script,
ActiveWorkbook.Save
MsgBox ("The " & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " Workbook was saved!")

and the WorkBook has been saved, when I click on the "X" in the upper right hand corner to close the WorkBook and exit Excel completely, I still get the window asking me if I want to Save the Workbook.

I even tried "swapping" your lines of code to:
MsgBox ("The " & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " Workbook was saved!")
ActiveWorkbook.Save
 
Upvote 0
Hi LazyBug

Thanks for your help.
After the macro prints and saves, I'd rather NOT automatically close the WorkBook. Just in case I want to look at something else.

But, even after changing to your script,
ActiveWorkbook.Save
MsgBox ("The " & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " Workbook was saved!")

and the WorkBook has been saved, when I click on the "X" in the upper right hand corner to close the WorkBook and exit Excel completely, I still get the window asking me if I want to Save the Workbook.

I even tried "swapping" your lines of code to:
MsgBox ("The " & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 5) & " Workbook was saved!")
ActiveWorkbook.Save
I have not been able to reproduce the situation in my file. Maybe you could post the macro text in full?
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,208
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top