workbooks.close statement?

vbacoder

Active Member
Joined
Jul 7, 2007
Messages
354
I'm using the command

workbooks.open filename:= "G:\....."

to open a workbook on my computer. Is there an equivalent statement to close the workbook after I have finished working on it?

Many thanks,

vcoder
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,952
Office Version
  1. 365
Platform
  1. Windows
I usually use:

ActiveWorkbook.Close
 

vbacoder

Active Member
Joined
Jul 7, 2007
Messages
354
Thanks jm14!

I tried various things like workbooks.close, but none worked. I'll try this.

vcoder
 

vbacoder

Active Member
Joined
Jul 7, 2007
Messages
354
I have used the ActiveWorkbook.Close statement, however, in some instances, I am asked (via dialogue prompts) whether I want to save the worksheet and whether I want to keep copied information in the clipboard for later use. I want to answer No, Yes, respectively, but I'm not sure how to invoke this in VB code.

I would be very grateful for any advice on this.

Many thanks,

vcoder
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
56,952
Office Version
  1. 365
Platform
  1. Windows
Sorry for the delay in replying, I have been on vacation.

The ActiveWorkbook.Close command has optional arguments, the first of which is whether or not you want to save changes. So if you want to answer "No" to that, it would look like this:
Code:
ActiveWorkbook.Close False

Alternatively, you can suppress all warnings by putting this line of code before the other statement:
Code:
Application.DisplayAlerts = False
This will suppress all warnings and choose the default. Just be sure to turn it back on afterwards with the command:
Code:
Application.DisplayAlerts = True

Regarding the clipboard, I am not sure what the default is. If it is to keep it, they the above will work for that also. You can confirm it either way by testing.
 

Greg Truby

MrExcel MVP
Joined
Jun 19, 2002
Messages
10,014
Unless you are opening this workbook and pulling stuff out or popping stuff in<sup>1</sup> and the immediately closing it (i.e. if the user gains access to or has the ability to examine the workbook being opened = user can interact with the Excel) then I would shy away from using ActiveWorkbook.

Better to use a global (not tested, just written "off the cuff"):
Code:
public g_wbToOpen as workbook
sub foo()
set g_wbtoopen = workbooks.open(filename:="g:\...")
end sub

sub oof()
if not g_wbtoopen is nothing then
   application.displayalerts = false
   g_wbtoopen.close false
   application.displayalerts = true
end if
end sub
<hr />1. "Popping stuff in", of course, doesn't make a lick of sense since you've told us you wish to close this WB w/o saving any changes. But I've seen odder stuff 'round here. :biggrin:
 

Watch MrExcel Video

Forum statistics

Threads
1,129,754
Messages
5,638,170
Members
417,011
Latest member
Amaden95

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
Top