vba code, closing woorkbook

isacp

Board Regular
Joined
Dec 16, 2004
Messages
135
my macro uses three workbooks and process them into a fouth. i want it to close the three workbooks and not save it. i have it in the code to close but every time i run the macro it asks if i want to save it is there a way to write in the code not to save it.

also what is the code for a save as
and then have it read a ans from a msgbox and use it as the save as name
 

Some videos you may like

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
isacp said:
i want it to close the three workbooks and not save it. i have it in the code to close but every time i run the macro it asks if i want to save it is there a way to write in the code not to save it.

I believe this should work:

<font face=Tahoma>Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN>

ActiveWorkbook.Close
<SPAN style="color:#007F00">'Code to close workbooks goes here</SPAN>

Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN></FONT>

also what is the code for a save as
and then have it read a ans from a msgbox and use it as the save as name

Try:


<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> test2()
<SPAN style="color:#00007F">Dim</SPAN> SaveName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>
    
SaveName = Application.InputBox("Name the file:", "Save File", Type:=2)
<SPAN style="color:#00007F">If</SPAN> SaveName = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'if cancel is clicked</SPAN>
<SPAN style="color:#00007F">Else</SPAN>
    ActiveWorkbook.SaveAs Filename:=SaveName
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 

HalfAce

MrExcel MVP
Joined
Apr 6, 2003
Messages
9,454
Assuming you're working with the active workbook, here are
several things you can do...

To close it without saving & no message...
ActiveWorkbook.Close saveChanges:=False
or just
ActiveWorkbook.Close False

To close it and save it (under its existing name)...
ActiveWorkbook.Close saveChanges:=True
or just
ActiveWorkbook.Close True

To call up the SaveAs dialogbox...
Application.GetSaveAsFilename

To call up the SaveAs and force a file saveas name...
Do
fName = Application.GetSaveAsFilename
Loop Until fName <> False
ActiveWorkbook.SaveAs Filename:=fName

Hope some of it helps,
Dan
 

isacp

Board Regular
Joined
Dec 16, 2004
Messages
135
thank you both for you help it is very usefull for maney f my macros.

kristy i have one question to add into the mac you gave me.
whe i run it it gets saved in mydocs is there a way to have a specified file path?
 

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
If you want it to go to the same directory every time, you can just hard-code it in, then the file should save there:

<font face=Tahoma><SPAN style="color:#00007F">Sub</SPAN> test2()
<SPAN style="color:#00007F">Dim</SPAN> SaveName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Variant</SPAN>

SaveName = Application.InputBox("Name the file:", "Save File", Type:=2)
<SPAN style="color:#00007F">If</SPAN> SaveName = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>
    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN> <SPAN style="color:#007F00">'if cancel is clicked</SPAN>
<SPAN style="color:#00007F">Else</SPAN>
    ChDir "full directory path goes here"
    ActiveWorkbook.SaveAs Filename:=SaveName
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 

Watch MrExcel Video

Forum statistics

Threads
1,123,324
Messages
5,600,956
Members
414,417
Latest member
Nobu

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