VBA- Closing Workbook Without Saving the File - excel

Hi I'm trying to close my workbook without saving it and I am using the code:
wb.Close savechanges:=False
However, it still closes my workbook and saves all the changes made. Is there a workaround for this or the reason why it keeps saving my workbook? Please help

Related

Exit application without closing all the files

I am trying to provide an option to my users to save and close excel application after they finish to complete a specific form. However, it is closing the whole Excel and not only the current Excel workbook that they just used. In other words, if they have 4 different Excel files opened, it will close all of them and not only the current workbook. What am I doing wrong?
This is the code that I am using:
ThisWorkbook.Application.Quit
ThisWorkbook.Save
You must not quit the Application - but close the workbook:
ThisWorkbook.Close SaveChanges:=True
will save and close the workbook.
If it is the only open workbook, Excel gets closed as well.
If not Excel and the other workbooks are still open.

closing all open workbooks without saving and then open an another workbook

I'am looking for the VBA-code for closing all open workbooks without saving and then open an another workbook.
Nows anybody the correct VBA-code?
I have now the follow code:
Private Sub Workbook_Open()
Dim WB As Workbook
For Each WB In Workbooks
If Not (WB Is ActiveWorkbook) Then WB.Close
Next
End Sub
This one i haven't tried jet. I try to make a macro dat starts every day one the same time and closed all workbooks. Then opens workbooks and update the data.
I have adjust WB.Close savechanges:=False to the VBA-code and its works perfect.
Thanks
In my opinion, using macro is not a good way to do this.
Check this out.
http://blog.contextures.com/archives/2013/04/25/close-all-files-in-excel-2013/
Or you could do this using cmd command like
taskkill /IM excel.exe /F
start excel.exe
or making a batch file or a vbscript doing the same.

Old workbook (once opened by VBA) keeps opening when I open another excel file

I am building a database in Access, for which I import data from an Excel workbook questionnaire. I have coded an Import-sub that selects, opens, retrieves the data from and finally closes the workbook.
The problem is this: for some reason, when I now open any excel workbook on my computer (at a time when neither Access or Excel is in use) some old version of the questionnaire keeps opening as well. This problem doesn't end by restarting the computer, but only by deleting that specific questionnaire-workbook. But then it starts happening with another workbook.
I have a theory that this might be because I - in my import-sub - have opened the questionnaire, encountered a run-time error which has ended the sub before it closed the workbook, and that somehow the workbook is still "open". Or that there still is a link active.
So I have two questions:
1.) Does anyone know how I can fix this problem?
2.) Is there generally any consequences of not closing a workbook that you open through VBA?
My relevant code is:
Dim MyXL As Excel.Application
Dim MyWB As Excel.Workbook
...
in between lots of stuff happening, several times an error occurs which interrupts the program.
...
MyWB.Close False
MyXL.Quit
Appreciate any help on this!
I did Encounter the same Problem and found out that Excel stores the files that open whenever you start Excel in a Folder (XLSTART). The path to mine was: c:\USERS\MyUserName\AppData\Roaming\Microsoft\Excel\XLSTART
As suggested by Ross McConeghy error handling may prevent such an occurrence. But the error already happened and the questionnaire, as you suggested, has placed that workbook in the Folder XLSTART. You have to delete it from that folder to fix the unwanted occurrence.
Your theory is likely. If you never display the Excel application, errors are encountered, and your code never closes the workbook it would be left open in the background and your code would most likely lose reference to it. The next time you open an Excel file the instance of Excel that is already open(but hidden from view) "picks up" the request and opens the file, also displaying the file that was still open from the macro.
1.) You should set up error handling so that the Workbook and Excel application are closed when there is a non-recoverable error.
Sub MySub()
On Error GoTo handle_Err
<... code ...>
Exit Sub
handle_Err:
MyWB.Close False
MyXL.Quit
End Sub
If you have other Error handling statements in your code always reset to On Error GoTo handle_Err instead of GoTo 0 etc.
2.) The only consequences of not closing the Workbook are the obvious ones- the system resources it's using and, if it is open for editing, no one else can edit it.
I had a similar problem and solved it a different way.
I found the connection to an external workbook and fixed it by going to Data > Edit Links.

Excel Workbook open macro reopens closed workbook

I do the following as a macro
Open a list of files
copy some values
Close them
After that when I exit and reopen the file that contains the macro, it also opens the files which I previously opened. even those I had used the app.workbook.close
I'm unable to find the problem out.
Where is the macro located? In a normal module?
At the end, seeing as how you've already pointed the variable at it, you may as well say
currentWB.close False
Then to close
Set currentWB = Nothing
Are there some links between the file that has the macro and the files it creates?

Creating a self-deleting macro in Excel 2007

I'm trying to create a Macro in Excel 2007 that will delete itself when it finishes running and close Excel. The reason I want to do this is that I am going to be sending the Workbook out to other people, and I don't want them to see security warnings about the Macros. Several versions of this Workbook will be generated, so this I don't want to manually run and remove the Macro for each one.
I've tried the following code. It runs without error, but does not actually delete the Macro. If I remove the line "ActiveWorkbook.Close SaveChanges:=True" the Macro is deleted, but this prompts the user to save the Workbook as Excel closes. Is there any way to do this without User interaction?
Dim ActiveComponent
Set ActiveComponent = ActiveWorkbook.VBProject.VBComponents("ModuleName")
ActiveWorkbook.VBProject.VBComponents.Remove (ActiveComponent)
ActiveWorkbook.Save
Application.Quit
ActiveWorkbook.Close SaveChanges:=True
Why not just move your macros to another workbook and have them operate on the workbook(s) you're sending out?

Resources