Protect excel sheet but allow data entry with form - excel

I have an excel form that input data, but i dont want the users to be able to play with the data once added to the sheet. I just want them to be able to add data with the form. Thing is, if I protect my sheet, I wont be able to add data via the form.
I can unlock the first row, making it possible to add data, and lock the rest of the sheet. But that only fix 90% of my problem.
I tried this code in ThisWorkbook :
Private Sub Workbook_Open()
Me.Worksheets("Sheet1").Protect UserInterfaceOnly:=True
End Sub
Then I locked my sheet and it's still fiving me error 1004.

The answer to my question :
Private Sub Workbook_Open()
Me.Worksheets("Sheet1").Protect UserInterfaceOnly:=True
End Sub
And then lock your sheet like you normally would.
IF you want to read more on the subject : cpearson.com/excel/Protection.aspx
Thank you #BigBen

Related

Is QueryClose the proper way of protecting a worksheet when closing a form in Excel VBA?

I have a workbook with many worksheets. Some of them must be protected, and some of them unlocked. The tricky part is that a userform created using Excel VBA will be used to add data to one of the protected worksheets, and the code fails when said worksheet is protected because it is supposed to add rows to a table to save the data.
I tried using the following code to fix this:
Sheets("Sales").Protect Password:="123", AllowInsertingRows:=True, UserInterFaceOnly:=True
Although after running it I can manually add rows through the user interface, it seems that the property I am trying to use only works that way, VBA code still can not and new rows to the protected worksheet.
So, the workaround I thought of is to unprotect the worksheet when the userform is initialized, like this:
Private Sub UserForm_Initialize()
Sheets("Sales").Unprotect "123"
End Sub
This way the userform can save all the data without any issues. However, my question is how to properly protected it again. There are two ways of closing the userform, one is with a cancel button that just runs the Unload Me piece of code, and the second one is using the X on the top right corner of the form. I would like to keep those two options available, but also make sure that the worksheet is protected again before the user can try to edit it (it is imposible to select anything with the userform open).
What I am thinking would be the best option is the following code:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Sheets("Sales").Protect Password:="123"
End Sub
After testing it I believe it works, but I am not sure if this is the proper way of doing this. Maybe there is a specific situation that I am not considering that would end up with the user being able to edit the worksheet that is supposed to be protected. Could please let my know if there are any flaws in my code?

Excel graphical glitches when moving between sheets with private subs?

I encountered an odd graphical glitch when running private subs on a sheet then using a macro to jump to another sheet with a private sub on it. Basically excel is getting slowed down (the drop down menu's in the ribbons get messed up as well).
IE:
Sheet 1 has
Private Sub Worksheet_Deactivate()
Sheets("Sheet1").Visible = False
End Sub
Sheet 2 has the above code as well except Sheet2 would be the one made hidden when deactivating the worksheet.
With a button placed on sheet1 which trigger the following macro
Sub Sheet1_Button1_Click()
Sheets("Sheet2").Visible = True
Sheets("Sheet2").Select
End Sub
For testing purposes I was just using another macro assigned button on sheet2 that jumped back to sheet one and found that caused the issue. Does anyone know what's going on here and how to prevent it? Maybe this is more of a Microsoft issue?
In my original workbook I had a private sub on a "Cost Estimations" sheet that would run some some code to un-hide used lines and re-hide unused lines in a table that was referencing another sheet. Then I had a macro assigned button on that same sheet that would open a normally hidden sheet with some more info on it. The "hidden" sheet had a private sub on it that automatically hide it when the user clicks off of the sheet just like the "Sheet1" in my example. Additionally in the original workbook it was causing all the information from "cost estimations" to display on the "hidden" sheet, but only if calculations were set to automatic. I was however unable to replicate that in my test worksheet.

Deleting rows on protected sheet

I have a worksheet that is protected; all cells are locked besides those that are intended to be edited by the user. I would like the user to be able to delete rows, so I enabled 'delete rows' when protecting my sheet. As expected I receive the error message "You are trying to delete a row that contains a locked cell..." I understand why this is happening as all cells outside the area I expect user input are locked.
I don't want to unlock all other cells as I don't want users to enter data in them.
I have researched the ability unprotect the sheet on a 'delete row' event but wasn't able to find such an event. I guess I can add a button next to each row that unprotects the sheet deletes the row and reprotects the sheet but was hoping for a more elegant/easier solution.
How can I delete a row in a protected sheet when there are locked cells in the columns to the right of my table?
You can have a button which calls a VBA function which does the deleting. And use
Protect UserInterfaceOnly:=True
when protecting the sheet.This way you'll be able to delete rows and perform other actions via VBA on a protected sheet.
You can normally achieve this via the following code:
Public Sub ProtectMySheet()
Sheet1.Protect UserInterfaceOnly:=True
End Sub
However, I've discovered this to be hit or miss sometimes. So I sometimes do this
Public Sub EditStuffInProtectedSheet()
With Sheet1
.Unprotect MYPASSWORD ' You can store your password in a safe location, or make it a constant (if you're okay with your End Users knowing it)
' ***Do Stuff***
.Protect MYPASSWORD
End With
End Sub

How to disable sorting in an Excel shared workbook

I would like to allow only the owner of a shared excel workbook to be able to sort data on the file. Whats the best way to do this?
If the main reason for this is not robust security, but the accidental resort that happens in a workbook shared with others this should work in the Worksheet_Activate event
This is not even attempting to hide the sorting prohibition
Private Sub Worksheet_Activate()
Dim WhoCanSort As String
WhoCanSort = ThisWorkbook.WriteReservedBy
If WhoCanSort = "Charlie" Then
ActiveSheet.Unprotect
Else:
ActiveSheet.Protect AllowSorting:=False
End If
End Sub
There is another approach that doesn't require a macro. If you use the "Allow Users to Edit Ranges" feature, you can keep the cells locked and the worksheet protected for most users, but define ranges of cells that can be edited by a particular user. See this article: http://blog.softartisans.com/2013/10/01/kb-sorting-locked-cells-in-protected-worksheets/

How to prevent user from deleting sheet, but leave all else open

I have developed an Excel Application in which useres can enter custom categories. I let the user call up and hide this data entry worksheet with a button in the custom ribbon.
Now I've realized that the user can accidentally delete these worksheets. How do I disable the delete command for this worksheet while leaving all else open?
I've been searching the web but have come up empty on this.
This is for Excel 2007
Thanks
Protect them.
Tools > Protection > Protect Worksheet.
Add the password and choose what actions your users should do in the sheets.
You can do the same using VBA too. Check the following link
Updated with a code for sheet level protect
You may put the following code in the sheet that you need to manage any mischief ;)
Private Sub Worksheet_Activate()
ThisWorkbook.Protect Password:="Password", Structure:=True
End Sub
Private Sub Worksheet_Deactivate()
ThisWorkbook.Unprotect Password:="Password"
End Sub
But you see, when you have a book with 100 sheets and if you want 50 sheets to be protected.
Then you gotta either save all the sheet indices into a very hidden sheet. Usee that list in a module level VBA code to trigger the protect. Because not everytime you will have sheets in asceding order. If sheet indices in an order you can simply iterate them.
Let me know if you like to have workbook level code as well.

Resources