I have created an add-in that sends and retrieves data from a database in order for this data to be used by our analysts.
To prevent changes made to existing data points I want to lock the cells containing this data. Initially, I did this by locking the range of the data and protecting the workbook, since otherwise the locking does not work. However, protecting the workbook also removes/limits a lot of functionality for the end-user, such as creating graphs, the auto fill function, changing the format etc. Since these and other functionalities are needed for the end-user, I cannot protect the workbook. Still, I want to lock the cell containing the data points.
So my question is, is it possible to lock the cells in a dynamic range (I have macros detecting the correct end column and end row of the data points) without protecting the workbook? If so, how? If not, would it be possible to detect changes in the dynamic range and show a messagebox that changes are not allowed in this specific cell/range and revert back to the old value of the cell? If so, how?
I have seen a few posts asking a similar question, but these were either not answered or the answer was not satisfying for my case (e.g. a macro implemented in the VBA project of the workbook instead of the VBA project of the add-in).
Thanks in advance for your answer(s)!
Kind regards,
Robbert
Use
ActiveSheet.Cells.Locked=False
Then Lock your range which you don't want to be edited using:
Range("A1:A4").Cells.Locked=True
Then protect the sheet
ActiveSheet.Protect Contents:=True, DrawingObjects:=False
This will only protect the contents that are Locked and still allow you to change values in the rest of the sheet and insert/delete charts. You may have to change extra settings with Protect. More details can be found here
You don't have to protect the Workbook, only the Worksheet.
To protect/unprotect a sheet via VBA, use Worksheet-methods protect resp. unprotect (you can apply a password).
To prevent a range to be modified, you have to set it's locked-property to True and protect the sheet (all cells have the propery locked set by default).
Be aware that, if a Range is locked and the worksheet is protected, you cannot modifiy the Range via VBA. If you want to do so, use the unprotect method at the top of the code (but don't forget to protect the sheet again when you're done). As an alternative, you can call protect with parameter UserInterfaceOnly:=True, but unfortunately Excel doesn't save this property. So if you save an Excel file where a worksheet is protected with UserInterfaceOnly:=True and reopen it, the sheet is protected also for VBA.
Related
I am wanting to put a notes section into a workbook, where notes can be added about a particular row in a worksheet. There will be lots of rows.
I want these notes to be uneditable once they have been written, so they can't be tampered with.
Previously I have done this by unprotecting, then protecting the worksheet to enter the note which is captured in VBA. However this workbook needs to be shared, which means you can't unprotect or protect a workbook either in VBA or manually.
Excel really isn't the right platform for this, as it's basically a small CRM, but unfortunately for the moment it's the only thing available and that can't change for now, so need to try and find a way to make it work, any idea's?
I have tried protecting the workbook with UserInterfaceOnly:= True, and that works great until the workbook is closed and re-opened then it fails and then can't unprotect or protect the workbook.
I created an excel file about costing a metal desking frame, it consist of several worksheets, there is one specific worksheet - a template - where the user can enter data - but this sheet must not be duplicated as it would mess up the program, I protected the sheet but when pressing Ctrl key at the same time mouse dragging the sheet to the right it duplicated the sheet! while protect workbook completely freezes all worksheets! I want to prevent copy (duplication) only on one sheet (the template). How can I do this
also How do I prevent this specific sheet from being accidentally (or intentionally) deleted?
What you need to do is lock/unlock cells as required, and then enable worksheet protection. Passwords are optional but recommended to prevent accidental unlocking.
An important thing to note before going any further is that there is no fail-safe way to protect an Office document (against neither accidental nor intentional abuse or distribution). Even Office document passwords are easily defeated.
If you are this concerned about this data, you need to make sure that you are backing it up regularly, and automatically, and keep several versions of backups... (not just the most recent!)
Protecting a worksheet from changes
When you protect a worksheet, all cells are locked by default, which means that they cannot be edited. To enable cells to be edited while leaving only some cells locked, you can unlock all the cells and then lock only specific cells and ranges before you protect the worksheet. You can also enable specific users to edit specific ranges in a protected worksheet.
Try this an an example of the most basic form of protection:
Make a backup copy of your workbook before proceeding.
Click the Review tab, and then click Protect Sheet.
For now, just ignore the check boxes at the bottom of the Protect Sheet window. Choose a password and enter it where prompted. (Do not use a password that you use for other things.)
Click OK. Enter the password a second time and click OK again. The worksheet is now protected.
At this point, experiment to see if you are able to make the changes you were concerned about, such and duplicating cells with "Ctrl+Drag".
This is the default setting for protection. You'll notice that you can't use any of the cells at all, even for "legitimate" purposes, however this can be easily rectified too (with the password!).
Removing workbook protection
When you want to re-enable changes to the worksheet:
Click Unprotect Sheet on the Review tab.
Enter the password you previously set.
...I literally already forgot the password I had set, so the unlocking request was denied:
It's okay, I made a backup first and had it stored in a safe location.
(Actually, it was a test workbook, and even if it wasn't, I could bypass password protection on any Office document in under 5 minutes. Hence the earlier warning...)
Protecting only certain areas or certain activities
In most cases, it's not feasible to have the sheet 100% protected at all times, effectively rendering it "read-only" and non-interactive. (If it were necessary, you're better off to print the worksheet and hand out ultra-safe paper copies.)
Protection can be customized in numerous ways, the most common being unlocking only the sections that the user needs to be able to change.
In the example below, I protect the whole worksheet (since cells are set to "Locked" by default) with the exception of the 2 cells that require data entry.
Personally I like to always shade the unlocked cells a different color, so that it's obvious to the users which cells they can/can't use.
There are too many options to go into them all here, but below are links some of the many sites with more information and examples about Excel workbook ad worksheet security, protection, and other safety concerns:
Office.com : Protection and security in Excel
Office.com : Protect a worksheet (Excel)
Office.com : Lock or unlock specific areas of a protected worksheet
Newco.co : Excel 365: How to Protect Cells in a Shared Worksheet
MSDN : Password protect workbooks and worksheets (Video)
I have a workbook I have created with lots of different formula's. My question is, how can I stop the end user from stealing the formula's? I have had a look at: Protecting Code in an Excel Workbook?
This is generally for VBA. Is there a way to stop users doing this, or is it just worksheet protect and cross your fingers?
You probably should be more specific on what formulas are you trying to protect: Excel Worksheet formulas of the VBA code?
In general, you can create a custom VBA Add-in (i.e. .xla file) and protect it with password as per Excel documentation. In case your major concerns relate to worksheet function, then you can include them in said VBA add-in using for example, Range.Formula (re: https://msdn.microsoft.com/en-us/vba/excel-vba/articles/range-formula-property-excel ) or Range.FormulaArray properties.
Hope this may help.
There is no way to fully protect formulas in Excel. For every password protection type, there is a way to break that protection. The only thing you could do if you really wanted to prevent end-users from getting the formulas is to hard-code the entire sheet before sending it to them. Of course, if the workbook relies on user input and using formulas to output an answer, that won't work.
Do you need the formulas protected for some sort of IP reason, or just to prevent people from messing them up?
This is my problem. I need my VBA macro to make a particular number of top rows (let say, N) in an Excel spreadsheet read-only, so a user would not be able to change them by mistake. Still, I need a user to be able to edit and change other rows in the same spreadsheet. If I do something like
Range("A1:J10").Select
Selection.Locked = True
ActiveSheet.Protect Contents:=True
then the whole spreadsheet is getting locked. If, on the other hand, I omit ActiveSheet.Protect Contents:=True line (do not protect the sheet), nothing is getting locked at all.
So, the question is: is it possible to block only the specified rows, while allowing user to edit the rest of them? I would appreciate the VBA code doing that.
By default all cells on a sheet are Locked=True, but this has no effect until the sheet is protected.
You will need to unlock the cells you want to stay editable before the sheet is protected.
I received a protected excel file which only allows me to add values or select from a drop down list. The file has lots of different tabs, all in different format and questions.
I then unprotected the workbook to make my life easier (i.e. copy, paste, make notes etc)
using the code found here:
http://uknowit.uwgb.edu/page.php?id=28850
Now i am looking for a way to transfer all the values from the unprotected file back to the original file they sent me as I cannot submit an unprotected file. It is too many questions to do manually.
What is the best way to do this in excel 2013/VBA?
Thank you
It looks like you're wanting to do this on a sheet that previously had a password you didn't have access to. Also, it seems that you can't ask the worksheet creator to simply remove the password.
If both these are true, the best solution would be to save the modified sheet with the same name and send that sheet. From what I've seen, VBA and developer tools don't include a way to copy values to locked regions in a password-protected sheet, without unlocking that sheet:
ActiveSheet.Unprotect 'method to unlock current sheet, password may be required
ActiveSheet.Protect 'same to lock again
But, once a sheet is unlocked, you could loop through all cells in the region you want to copy from, and copy those values to the new sheet / workbook.
Please let us know if there's something else, or if something needs to be explained in more detail.