I have a spreadsheet with several sheets, which is protected except where I want people to make changes, and all is password protected. I am trying to make a command button so others can view the data, but can't make changes to the cells. Here is what I have (not working quite right).
Private Sub mdRead_Click()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
'To open wookbook as read only, while proctecting changes.
Worksheet.Unprotect = True
Worksheet.Range("C10:I23,L10:R23,C25:I36,L25:R36,C45:I58,L45:I58,C60:I71,L60:R71").Select
Selection.Locked = True
Next ws
End Sub
Having set the Locked property, you might want to protect the sheet again.
Related
I implemented smart dropdown (using Active X controls) in my sheet, so it is protected.
This functionality is not able to work because of protection.
When I right click on the sheet and Unprotect it, providing the password, it works.
I do not want the user to do this manually. I want that when user opens the sheet the sheet should get unprotected using VBA code.
The entire cells in the sheet are Locked (Format cells-> Protection -> Locked marked).
I cannot untick the Locked mark, as there are some validations which gets failed and not allow me to upload such template.
Below is an example as I cannot share the application code.
VBA code:
In sheet1-> name of sheet is "tab0"
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Unprotect Password:="xxx"
Inside ThisWorkbook I have this
Private Sub Workbook_Open()
ThisWorkbook.UnProtect Password = "password"
End Sub
A late input to this question:
I've been experiencing similar problems with Unprotecting sheets, and ended up doing aa fair bit of testing to find what works and what doesn't (always!).
The problem appears to be when assigning the sheet to an object and using the object's dot notation to unprotect e,g.:
'Setting the worksheet (globally) in sub Main()
Public goXsTT As Worksheet
Set goXsTT = goXbWb.Sheets(2)
'Somewhere else in the code
goXsTT.Unprotect
'This ***may*** fail, whereas:
Sheets(2).Unprotect
'Works!
No idea why this is the case - just pointing out that it's a workaround if you're stumped, as I was. Hope it helps. DG
I have a workbook with multiple tabs. All tabs are protected. Some are fully protected while others are partially protected (that is, column and row size can be adjusted). There is one page of inputs. When the macro button is pressed, all the other tabs are populated. In order to allow the tabs to be populated by a macro even where the cells are locked I have used the following code on Workbook Opening:
Private Sub Workbook_Open()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
wSheet.Protect Password:="Secret", _
UserInterFaceOnly:=True
Next wSheet
End Sub
However, this resets all the tabs to be fully protected (whereas some only need to be partially protected).
Is there a way around this?
Many thanks in advance
The Sheet.Protect seems to have a parameter for
AllowFormattingColumns Optional Variant
True allows the user to format any column on a protected worksheet. The default value is False .
Have you tried this?
Trying to create a workbook that gets data from ACCESS. I can open the workbook and was adding some code to auto open in excel. But when i tried to edit it keeps telling me that I must unhide hidden work sheet. And the unhide command is grayed out. When I first set it up I selected Personal Workbook, which I think applies to any book I open. Along the way I kept deleting workbooks in order start over, so I think there is nothing really to delete. I wanted to add this code to to auto start but I can't get to the code. The follwing code is supposed to unhide all hidden workboks/sheets:
Sub Viewit()
Dim Ws As Worksheet
Application.ScreenUpdating = False
For Each Ws In Worksheets
Ws.Visible = True
Next Ws
Application.ScreenUpdating = True
End Sub
Looking for a better solution and what I am doing that is wrong.
Thanks
There is a very hidden level :
ActiveWorkbook.Sheets("sheet name").Visible = xlSheetVeryHidden
Or xlSheetVeryVisible for the opposite effect
Note this level is only controllable through vba, but functions can work with cells on very hidden sheets.
Is there a way to have the tab color change depending on whether a sheet is protected or not? Eg. protected = green, unprotected = red.
I'm trying to find an easy way to visually spot which sheets are protected. I have multiple sheets, some I need protected and some I need unprotected. I edit the protected sheets on a daily basis but I need to protect them when saving so other users don't accidentally edit the data.
Using what #BruceWayne said. You just need to loop through all your worksheets and check if they're protected. The below code will loop through all then color Green if it's protected or Red if it's unprotected. You can change the event to trigger on whatever action you need. Be sure to put this code IN the workbook and not a module.
Private Sub Workbook_Open()
Dim wbk As Workbook
Dim ws As Worksheet
Set wbk = ThisWorkbook
For Each ws In wbk.Worksheets
If ws.ProtectContents = True Then
ws.Tab.Color = vbGreen
Else
ws.Tab.Color = vbRed
End If
Next
End Sub
I am new in using Excel VBA. I have an Excel VBA code that will duplicate a certain sheet for n times. I want to prevent user from changing the workbook structure manually (such as adding new sheet, duplicating sheet, or moving sheet), but I still want the VBA to be able to run.
How should I do it?
I was hoping you could use UserInterfaceOnly but that appears to be only worksheet level and not workbook level - it locks the worksheet for users, but allows VBA code to make updates.
The only other way I can think of is to lock and unlock the workbook as needed:
Sub Test()
Dim wrkSht As Worksheet
UnlockWorkbook
Set wrkSht = ThisWorkbook.Worksheets.Add
LockWorkbook
End Sub
Sub LockWorkbook()
ThisWorkbook.Protect Password:="aaa", Structure:=True
End Sub
Sub UnlockWorkbook()
ThisWorkbook.Unprotect Password:="aaa"
End Sub