Vba code to close workbook.

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,251
Office Version
  1. 2007
Platform
  1. Windows
Hi,

I click OK on a Msgbox & at the same time i wish to close the workbook / save changes.

The workbook is called TAX YEAR & the worksheet is called Sheet1

I am using this line of code below BUT it only closes the worksheet Sheet1
I am trying to close the workbook that the worksheet is with in, called TAX YEAR

Can you see why it doesnt work for me.
Thanks

Rich (BB code):
ActiveWorkbook.Close SaveChanges:=True

Also this didnt work either.

Rich (BB code):
Workbooks("TAX YEAR").Close SaveChanges:=True
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
In the 2nd example, you're missing the file extension (xls, xlsx, etc.)
 
Upvote 0
Hi,
That didnt make any difference & did the same as in just the worksheet NOT the workbook is closed.
See before & after screen shots.
Thanks
 

Attachments

  • AFTER.jpg
    AFTER.jpg
    57.6 KB · Views: 9
  • BEFORE.jpg
    BEFORE.jpg
    133.4 KB · Views: 9
Upvote 0
Sorry, works for me. Maybe it has to do with your versions. 2007 is pretty old stuff ;)
Might help others to help you if you clarify if the code is in the workbook you want to close, or if it's in another workbook. Perhaps
Application.Workbooks("Tax Year.xls").Close SaveChanges = true
That assumes it is xls, not xlsx.

Looks to me as if you're saying the application doesn't close, not the workbook. AFAIK, an open workbook must show at least one sheet. When there is no visible sheet, it looks to me as if your workbook closes but Excel remains open.
 
Upvote 0
The file is called Tax Year.
I open this file & have 1 worksheet of which is worksheet1.

When I run the code I want what has just be opened to close.
The file Tax Year has an extension of .xlsm
 
Upvote 0
ipbr21054 it seems like your application is not quitting when you close your wb. My guess is that you have an addin that loads when Excel starts and your wb opens and this remains loaded after saving and closing the wb. This keeps the application running so it appears that your sheet has "closed" but what U are seeing is the addin wb. I have no idea how to close the addin (and application) without uninstalling the addin. HTH. Dave
 
Upvote 0
Hi,
Ive looked into what ADD Ins i have installed etc & its only the Xl2bb from this group.
I deselected it etc & ran the code again.
Its still the same so not that.

I think i will leave this as ive watched a few videos now & dont see any where the whole workbook close & only the worksheet.

Many Thanks
 
Upvote 0
You are welcome and thanks for posting your outcome. I have a custom addin installed and was able to replicate your results. If you go to the VBE, you will see in the project explorer if there are any addins loaded. It will show more than 1 VBAProject and will indicate the name of the addin. Maybe check your task manager to see if you have more than 1 Excel process running? However, I'm not sure how this would cause your results? Beyond this, I' sorry have no further suggestions. Perhaps other will be able to help. Good luck. Dave
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,545
Members
449,317
Latest member
chingiloum

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