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

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

Related

Macro not launching on workbook open

I have a workbook that I've been using an sub auto_open macro on and it's been fine.
However I am now trying to open it from another workbook and the auto_open macro doesn't work. It just opens and no macros run.
If I add a Workbook_open in "This workbook" to run the same macros it works fine. However if I run the workbook normally (outside of the link) it now opens and doesn't run any macros.
Weirdest thing is if I allow both auto_open and workbook_open it runs twice which is not what I want obviously.
Private Sub Workbook_Open()
StartMacro
End Sub
Public Sub Auto_Open()
StartMacro
End Sub
My ideal would be to have either as long as it will open when launched normally or via a link in a workbook.
Any ideas why I'm getting these issues?
auto_open subs needs to be in a module, not in an Excel object (like a sheet's code, nor ThisWorkbook). Here is further reference about auto-startup options in Excel.

MS Excel User forms vba

Hi "im kind of new with excel VBA. I'm trying to do something simple as creating a USERFORM1 in VBA and showing it when workbook opens. I've looked it up online but for some reason something is not working.
I open excel, go to developer, create a userform1, add some stuff to it.
I open code for THISWORKBOOK and under Open procedure I type
Private Sub Workbook_Open()
UserForm1.Show
End Sub
Then I save it as Macro Enabled and when i open it, nothing happens. What is going on? I know this is a silly question but am i doing something wrong?
In trust center I didn't have macros enabled. I did that and everything seems to work perfectly.

Excel workbook won't close

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.

Excel does not completely close when using Workbook.Close

In the macro I'm running, I end with closing the last Excel Workbook that's open. When I run it everything closes except one window which doesn't have a spread sheet, just a light-blue backdrop. Am I using a wrong command or is there just no way to close all open windows?
Here is a post asking my exact question, but the solution isn't working for me.
Here are the last two commands that I'm using:
'
' Closes Populated JD Form
'
Workbooks(name).Close SaveChanges:=False
'
' Closes Transfer Template
'
Workbooks("Transfer Template.xlsm").Close SaveChanges:=False
Thanks in advance!!!
Don't turn the display alerts off as it will suppress any genuine questions.
Use this instead
ActiveWorkbook.Saved = True
Application.Quit
This will stop Excel for asking about saving changes as it now thinks that it has been saved already. A good example of this is your personal macro workbook, I use mine a LOT and am always creating or modifying code in there. If I turn the alert off and there are changes in my personal workbook, I lose them all.
Try not to bulk fix possible scenarios where possible. Another example is errors, have an error trapping routine and deal with it as needed as opposed to a blanket statement of on error resume next.
To make sure the question doesn't remain unanswered, using both suggestions
(credit to #DeanOC and #user1274820)
'Closes Populated JD Form
Workbooks(Name).Close SaveChanges:=False
'Closes Transfer Template
Workbooks("Transfer Template.xlsm").Close SaveChanges:=False
With Application
.DisplayAlerts = False
.Quit
End With
Once I removed atpvbaen.xls from my references, application.quit worked as advertised.
I was tearing my hair out - until I realized I had atpvbaen.xls set as a reference - which was ANOTHER excel thread left alive. I didn't need it for this application, so I disabled it. In the VB Editor, Tools => References un-check atpvbaen.xls. If I had needed it, maybe shutting that one down deliberately would have worked.

excel vba - only enable cell edits through code

I have an excel tool that gathers information from a user based on their login information. The information is stored in cells on one of the worksheets. If I wanted to lock these cells so they can't be updated manually, how can I go about doing that? If someone else logs in, obviously these cells would change. I hope to do it through VBA.
I only want to lock 6 cells... everything else should be editable...
Thanks
You can protect the worksheet with a password like this:
Private Sub Workbook_Open()
Sheets("sheetName").Protect Password:="YourPassword", UserInterfaceOnly:=True
End Sub
That will prevent users from manually making changes to the worksheet without entering the password. Your VBA code will still be able to make changes because you've set UserInterfaceOnly to True.
Note that users could easily view this password by navigating to the code through the Visual Basic editor. You can password protect the code as well, though: just right-click on the module, click on VBAProject Properties and go to the Protection tab.
See this page for more information: Excel VBA: Macro Code To Run Macros On Protected Worksheets & Sheets.
If these cells are the only ones on the sheet you want to protect, then just change the cell properties of the remaining cells by changing Locked property to false and leave the cells in question as Locked then protect the sheet using UserInterfaceOnly set to true (but realize that this doesn't work for all possible macro changes, so I usually avoid it.)
There are other methods that could work, but I think this is the best solution for you. If not, please add a comment to let me know.