Excel workbook won't close - vba

I have an Excel workbook which I have problems closing properly.
Only if I press the little "x", it closes the Excel workbook, but not the Excel application. When I press the big red "X", the Excel application closes. So this works...
BUT...
if I start by pressing the big red "X", nothing happens except that all the ribbon tools is greyed out, and so is everything in the menu under the Office-circle in the top left corner of the application.
I can close the workbook by pressing the little "x", but I'm still not able to close the Excel application, and I have to "kill" it in the Windows Task Manager.
A few observations:
If I, before I open the Excel workbook, open an existing Excel workbook, and then opens the specific workbook, it can be closed properly pressing the big red "X" (only when the existing workbook is still open).
The problem ONLY exists for this specific workbook, so I must have made something which cause the problem.
The workbook has VBA code, which could be the problem.
The problem is that I have many users using this workbook, and would like to have it work without these problems.
I really hope someone can help...

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Cancel = True
End Sub
Is an example of a code that would produce the issue you're having.
To solve the "issue" open the VBE (Alt+F11) and Break the execution (pause button).
I personally think the Cancel declaration is linked to a status of the workbook, make sure you have done what you were supposed to do with the workbook.

Related

Excel Unprotects Sheet after VBA Code Crash Despite File Not Being Saved

I have an excel book used by colleagues that is protected when they open it, when testing a new feature I was developing I have encountered an odd quirk with Excel that I don't understand:
When clicking a button linked to a Macro, the VBA code begins by unprotecting the workbook, like this:
Sub ButtonClick()
Dim userrange As Variant
Dim rrow As Range
Dim teeth As Range
' unprotect sheet
ActiveSheet.Unprotect ("password")
Application.EnableEvents = False
The macro then crashes (I know why, that's not the issue here). I then press end on the error message pop up, and close excel without saving the file. When the file is reopened, the book is unprotected.
Essentially, the code crashes before it gets here:
' protect sheet
ActiveSheet.Protect ("password")
Application.EnableEvents = True
Can I ensure the excel file is still protected when reopened, even when the VBA code crashes after unprotecting it?
The reason this is an issue is that I have some existing functionality in the workbook that only works properly when the workbook is protected. So if somebody crashes the program then tries to reopen it, they cannot use it normally again without my input.
I find it odd that Excel 'saves' the fact that the workbook was unprotected, even if I close the file without saving anything. I'm aware some 'stuff' happens in the background when running a VBA code, for example the undo stack is cleared, I'm guessing something in the background is recording the fact the worksheet has been unprotected even if I don't save the file? I'd like to understand the mechanism, if anybody has an explanation of how the protection status is recorded, it would be appreciated.
As a workaround, you could ensure the file is protected at open using the Workbook_Open event (add to ThisWorkbook module). This doesn't explain how the workbook currently remains unprotected, but you should be able to circumvent that.
Private Sub Workbook_Open()
' Ensure sheets are protected at open
ActiveSheet.Protect "password"
End Sub

Selecting and switching to veryhidden Sheet not working properly

I have been writing a few macros lately to navigate around sheets / change visibility / import-export data etc. I normally just embed the macros as buttons on the sheets.
Normally this works well, however, I do keep experiencing an issue where with a macro, or a userform I unhide a veryhidden sheet, select it and exit the macro, or form.
I do this though via:
With Sheets("Sheet1")
.visibile = xlsheetvisible
.activate
.Range("A1").select
End With
When I then try to manipulate the sheet - e.g. type in a cell, delete data from cell, or insert / delete rows using the GUI rather than doing it via code, the operations happen on the original sheet with the button that called the form or macro. rather than the new one....
Is anyone else experiencing this? Am I doing something wrong?
Give worksheet object for cells while referring the editing
for ex... sheets().range().paste
or activate the sheet which you want to manipulate before your code (which manipulates the sheet)
Found the Solution!!!
Upon further investigation it seems like this is only broken in Office 2013. I tried it on Office 2010 and it worked fine.
The solution is to invoke the vbModeless command after Userform.show so that would be:
Userform1.Show vbModeless
Not perfect, if you want the user to dismiss the Userform before going back to the worksheet, but hey -it's a workaround :)
Let's hope Office 2016 will fix the bug (I'll be upgrading later this month)

vba programatically have excel forget window structure

In Excel 2013, Excel likes to remember Excel windows (not worksheets) that were open, and when the workbook is opened again, also open up those windows:
From user interface: With a new workbook, Ribbon Tab "View" and then "New Window". Now two windows are open. Edit at will. Save the WorkBook. Then when that workbook is opened again, both windows are opened.
I'd like to prevent this this programatically within VBA, as part of WorkBook_Open I suppose, so that just one window would open. How can I do that? I've tried closing all windows using WorkBook_Close, that didn't work.
Just loop through the Windows collection and close them until you only have one open:
Private Sub Workbook_Open()
Do While Me.Windows.Count > 1
Me.Windows(1).Close
Loop
End Sub

View the Visual Basic Code (VBA) when the workbook is closed on launch of the same workbook

By mistake I wrote code that will close the workbook when it is opened.
Because of this I am unable to see the code in order to fix it. How do I edit the code?
open Excel, go in and disable macros from the Trust Center, then open your workbook and remove the offending line of code.
http://office.microsoft.com/en-gb/excel-help/enable-or-disable-macros-in-office-documents-HA010031071.aspx
Try opening some workbook and click alt + F11 to see the macro window. Then open your workbook and it should be ok.
Alternatively, try switching off the macros in Excel so that no macros could run. Then open your workbook.

View Code When Modal Dialog Is Open In Excel

This may be a stupid question, but I feel like I'm in a pickle. I have a modal UserForm that opens when an Excel workbook is opened. When the UserForm is closed, the Excel workbook is saved and closed. I need to be able to view my code, but I can't seem to figure out how to do that because if I close the modal dialog box, the workbook closes. Does anyone know how I can view my code? I really apologize if this is a stupid question, but I can't seem to figure it out.
Thanks for you time and effort.
Without restarting the workbook i.e when the userform is shown in modal, you can use CTRL + Shift + Pause/Break to get into the VBE
Depending on laptops the key combination might change. Here is another which you can try.
Fn + Pause/Break
Hold the shift key when opening the workbook. This allows you to open office applications with macros not running and can be useful in situations like this.
Then view the macros (hit Alt+F11 to open it this editor).
The two other suggestions are good. For easiest debugging, I'd put the code that opens the userform in a separate routine and then call that routine from Workbook_Open. That way you can run and debug your code without having to re-open the workbook.
Then your ThisWorkbook module might look something like this:
Private Sub Workbook_Open()
MyUserformProcedure
End Sub
Sub MyUserformProcedure()
UserForm1.Show
End Sub
You could then comment out the line in Workbook_Open and call MyUserformProcedure, and uncomment the line when you're finished debugging.