I want to check if Excel worksheet is protected or not. I know that I can check .protectcontents, but what if worksheet is protected like this:
activesheet.protect contents:=false
.protectcontents returns false but shapes are locked.
Please help.
The three protection properties of a worksheet are the following:
Sheets(1).ProtectContents
Sheets(1).ProtectDrawingObjects
Sheets(1).ProtectScenarios
You can check whether both 3 are False. If this is the case, it is not protected.
.ProtectionMode says whether "User-Interface-only" protection is turned on. You may add it to the function, if you need it. MSDN - ProtectionMode
Related
I've seen many posts on here and elsewhere about using the following code in the ThisWorkbook Object in order to protect your workbook but still allow macros to run:
Private Sub Workbook_Open()
ActiveWorkbook.Protect UserInterfaceOnly:=True
End Sub
However, the protect function tooltip that pops up only offers three arguments Protect([password], [structure], [windows]). Therefore, when I open the document I get the following error
"Compile Error: Named argument not found"
in regard to UserInterfaceOnly:=True.
Am I using the wrong protect function, or is there something else I'm missing? I want to allow users to input data into the workbook using forms/macros, but I don't want them to be able to manually change any cells.
It almost seems as if the protect function that I'm running has different arguments than many of the other protect solutions I've found. Could this be a version issue or something else?
The suggestion provided by Tim Williams worked. Protect doesn't work the same way at the workbook level, therefore I needed to write individual code for each sheet I wanted to protect. I used the following code to protect the sheets but allow forms to populate:
Private Sub Workbook_Open()
Worksheets("Sheet1").Protect UserInterfaceOnly:=True
Worksheets("Sheet2").Protect UserInterfaceOnly:=True
Worksheets("Sheet3").Protect UserInterfaceOnly:=True
End Sub
I'm trying to protect a WorkSheet allowing the insertion of images. Then I learned how to protect a WorkSheet with VBA code, but the WorkSheet aren't allowed to insert images.
When I manually protect the WorkSheet I discovered that if I check the option "Modify Objects", the WorkSheet allows the insertion of images.
Now, I'm searching about of parameters of the Protect method, then I have the following code:
With Sheets("Sheet1")
.Protect Password:="password", DrawingObjects:=True, Contents:=False, Scenarios:=False
End With
I assumed that the code occupies the parameter DrawingObjects, which should allow the insertion of images, but the code doesn't works properly, the WorkSheet is protected, but still doesn't allow the insertion of images.
I believe your code protects DrawingObjects, rather than allowing them to be inserted. I think it should be DrawingObjects:=False in order to permit this to work.
Whenever I use VBA with a protected worksheet, I first unprotect it, do whatever I want in VBA, then reapply the protection. Or at at least, remove specific protections and then reapply them.
Private Sub Workbook_Deactivate()
ThisWorkbook.ActiveSheet.Selection.Copy
End Sub
I'd like to copy the selection in the deactivated workbook. But this doesn't work, indeed, I got a run-time error.
Damn! It is possible!
Private Sub Workbook_Deactivate()
ThisWorkbook.Windows(1).Selection.Copy
End Sub
Explanation:
Both ActiveSheet and Selection are children objects of the Application object, not the Workshet or Workbook objects. (See MSDN: Selection and ActiveSheet.) Selection means the currently selected item in the active window. Same for ActiveSheet.
Now it turns out that you can specify a window object for both of them. Normally every Workbook has one window, but you can create multiple windows for each open workbook. Anyhow, every open workbook that you can click and select will have at least one window. This is whateverworkbook.Windows(1).
ThisWorkbook always references the workbook running the macro (that means the workbook actually containing the VBA code). In your case, that is the one you are leaving. (Unless you want the macro to run from any workbook you are deactivating, in which case you'd have to come up with a custom event handler that catches each workbook deactivate event...)
And that's it, really. You can reference a selection in another workbook, you just have to attack it through it's Window.
Thanks for the good question, I have learned something very valuable today. :)
I have the following piece of code, which removes filters and then re-applies them from selected cells:
Range("A10:AM10").Select
Selection.AutoFilter
Selection.AutoFilter
The problem I have is that when the sheet is protected and I try to run the code it errors, so is there a way of allowing filters to be turned on and off on a protected sheet?
How are you?
It will depend on the setup of the protection. When you protect a worksheet, there is an option to allow filtering (AutoFilter).
If you're the one who's protecting the sheet, you can use any of the worksheet's protect method's parameters to set the protection options. Here are the protect method's parameters:
sheetName.Protect(Password, DrawingObjects, Contents, Scenarios, UserInterfaceOnly, AllowFormattingCells, AllowFormattingColumns, AllowFormattingRows, AllowInsertingColumns, AllowInsertingRows, AllowInsertingHyperlinks, AllowDeletingColumns, AllowDeletingRows, AllowSorting, AllowFiltering, AllowUsingPivotTables)
If you want to be able to implement filters only by code, then you can set the UserInterfaceOnly parameter to TRUE. The protection will block only the users changes. Code changes will be fully allowed.
sheetName.Protect UserInterfaceOnly:=True
Or, if you want to allow users to filter the contents of the worksheet from the interface, just set the AllowFiltering parameter to TRUE as well as the UserInterfaceOnly parameter.
sheetName.Protect UserInterfaceOnly:=True, AllowFiltering:=True
Your code should work with both options.
Hope it helps!
If I use this code to protect a worksheet, how can I make it so that the user still can copy the cells? And can you specify certain cells that should NOT be protected, or at least the user should be able to edit them?
Worksheets("EKONOMI").Protect UserInterfaceOnly:=True
You can define if user is allowed to select cells in protected sheet by using
Worksheets("EKONOMI").EnableSelection = xlNoRestrictions 'worksheet has to be protected for this to take effect
To make user able to edit certain cells you have to unlock the cells before the protection. For instance you can use the following to make Range C3 as unlocked cell
ActiveSheet.Range("C3").Select
Selection.Locked = False
Sheets("EKONOMI").Activate
ActiveSheet.Unprotect Password:="123"
'ActiveSheet.Protection.AllowEditRanges(1).Delete
ActiveSheet.Protection.AllowEditRanges.Add Title:="Range1", Range:=Range("A1:A10")
ActiveSheet.Protect Password:="123"
Please find the above code to protect sheet by allowing user to edit patricular range of cells.
ActiveSheet.Protection.AllowEditRanges.Add Title:="Range1", Range:=Range("A1:A10")
Mention the range name and range size for the user to edit.