I have to protect columns A:O from being edited in a worksheet I'm creating, but it doesn't seem to be working properly.
Set Wb = Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
With Wb
With .Worksheets("Sheet1")
.Cells.Locked = False
.Columns("A:O").Locked = True
End With
Why are those columns able to be edited after I run the macro?
Rest of script underneath (including saving):
.SaveAs strNewPath & strFileName, Password:="password", FileFormat:=51
.Saved = True
.Close
End With
Set Wb = Nothing
End If
Next
I'll just add on with an answer to clarify things. The Locked property simply indicates whether or not the cells is modifiable when the sheet is protected. You can protect a sheet by going to the Review tab in the Excel toolbar, then selecting Protect Sheet.
Alternatively, you can protect and unprotect within your code. For example:
Sub Protect_Sheet()
Sheet1.Protect "Password"
End Sub
Sub Unprotect_Sheet()
Sheet1.Unprotect "Password"
End Sub
You can call these methods within a larger method if you want to perfrom some action on locked sheets, then protect the sheet.
When the code start import:
Sheet1.Unprotect "Password" '<- Unprotect the sheet in order to Unlock sheet and give the opportunity to the code to work.
and when the code finish import:
Sheet1.Protect "Password" '<- Protect the sheet in order to lock the sheet
Related
I want to copy a hidden worksheet using VBA. However, When it run the vba code, all the copied worksheets are hidden as well, may I know is there any method to copy the worksheet and the new created worksheet is not hidden? My VBA code is as follows:
Sub CopySheet()
Sheet6.Copy After:=Sheets(Sheets.Count)
End Sub
There are two stages of Hidden, xlSheetHidden and xlSheetVeryHidden. On my Excel 365 your code worked for normal Hidden sheets and crashed for VeryHidden sheets. In neither case was the copy hidden. However, the following code will unhide the sheet, create a visible copy and hide the original again to the same level as it was before, all of that invisible to the user. This code can therefore be used for copying any sheet, regardless of the setting of its Visible property. It should work also on older versions of Excel.
Sub CopySheet()
Dim Visible As XlSheetVisibility
Application.ScreenUpdating = False ' hide the action from view
With Sheet6
Visible = .Visible ' record the sheet's visibility setting
.Visible = xlSheetVisible ' make the sheet visible
.Copy After:=Sheets(Sheets.Count) ' create a copy (the copy will be ActiveSheet)
.Visible = Visible ' reset the sheet's Visible property to what it was before
End With
Application.ScreenUpdating = True
End Sub
Please, try:
Sheet6.Copy After:=Sheets(Sheets.count)
Sheets(Sheets.count).Visible = xlSheetVisible
Sub CopySheet6()
Sheet6.Copy After:=Sheets(Sheets.Count)
Worksheets("Sheet6 (" & Sheets.Count - 1 & ")").Visible = True
End Sub
I Have created a data entry form in excel using VBA , I want to protect te database sheet to have manual entry but to allow save the data , edit the data and delete the data or row via vba userform.
I have coded the below written to protect the sheet.
Sub Workbook_Open()
Worksheets("Database").Protect Password:=True, UserinterfaceOnly:=True
End Sub
It is working fine but after some time it is giving me run time error 1004.
Along with this it is not allowing me to delete the previous data using delete button
When you edit sheet even through with a VBA userform, sheet needs to temporarily unprotected. You can do it like this:
' Unprotect
Sheets("SHEET").Unprotect password:="PASSWORD"
'
'your code here
'
' Protect
Sheets("SHEET").protect password:="PASSWORD"
Delete Rows on Protected Worksheet
If you are prepared to unlock entire rows, then you could prepare the worksheet with something like the following code.
It is not to be used in the Workbook_Open event code; it is just some code to prepare the worksheet.
Note AllowDeletingRows:=True.
But, you cannot delete a row if all of the cells are not unlocked.
The issue with rows (similarly with columns) is that if you delete one, the next row 'lands in the spot' of the deleted one. So e.g. if after using the example code the user deletes any of the allowed rows, than he wouldn't be able to delete row 50 which might just be the behavior that you require.
The Code
Option Explicit
' Remove this Sub when done.
Sub Allowed()
Const AllowedRows As String = "2:50"
Const pWord As String = "123"
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Database")
With ws
.Unprotect pWord
.Cells.Locked = True ' Lock all cells.
.EnableSelection = xlNoRestrictions
.Rows(AllowedRows).Locked = False ' Unlock allowd rows.
.Protect Password:=pWord, DrawingObjects:=True, Contents:=True, _
Scenarios:=True, AllowDeletingRows:=True
End With
End Sub
I know there are lots of questions on this, which I have read - but none seem to give me the code I need to make this work.
I have a number of buttons that I have placed in the Ribbon of my excel sheet. These are attached to macros that copy sheets onto another sheet, as an example
The macro is ran by pressing the button:
Sub btnSheet1_onAction(control As IRibbonControl)
mFunction.CopySheet1toSheet2
End Sub
The macro is contained in my mFunction module as :
Public Sub CopySheet1toSheet2()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
ws.Cells.Copy Destination:=ThisWorkbook.Sheets(2).Cells
End Sub
Now.... I need to protect items/cells in sheet 1 and 2. When I protect the sheets the macros make excel crash - no runtime errors or anything.
I have inserted the following code into the 'ThisWorkbook'
Private Sub Workbook_Open()
Sheets(1).Protect Password:="secret", UserInterFaceOnly:=True
Sheets(2).Protect Password:="secret", UserInterFaceOnly:=True
End Sub
But it still doesn't work - I have also tried with the following code in the mFunction module
Public Sub CopySheet1toSheet2()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets(1)
ws.Unprotect Password = "secret"
ws.Cells.Copy Destination:=ThisWorkbook.Sheets(2).Cells
ws.Protect Password = "secret"
End Sub
But that doesn't seem to work either - I am guessing it might be something to do with the fact that the macro is copying the sheet into another sheet that is locked also?
I should also note that there are other sheets in the workbook that are protected, but that do not have macros attached to them, so they stay protected, could this be causing an issue?
Some help would be greatly appreciated!!
UserInterFaceOnly
When you save a Workbook with sheets that have been protected using UserInterFaceOnly, this property is removed on the file that is saved. So on reopening the file the sheets will remain protected but can not be changed programmatically either.
So, regarding this piece of code, which on first glance appears to do exactly what you need:
Private Sub Workbook_Open()
Sheets(1).Protect Password:="secret", UserInterFaceOnly:=True
Sheets(2).Protect Password:="secret", UserInterFaceOnly:=True
End Sub
.. if you save and reopen your file, when your above Workbook_Open() runs it will fail to set the protection as there is already protection in place.
The workaround is to include lines for each sheet that remove any protection in place first. Then you can set it again correctly - like so:
Private Sub Workbook_Open()
Sheets(1).Unprotect Password:="secret"
Sheets(2).Unprotect Password:="secret"
Sheets(1).Protect Password:="secret", UserInterFaceOnly:=True
Sheets(2).Protect Password:="secret", UserInterFaceOnly:=True
End Sub
This should then allow your copy code to run without issue as I can't see much wrong with that part at all.
Incidentally, if your passwords are the same, you could tidy it up slightly with:
Private Sub Workbook_Open()
Dim sh As Worksheet
For Each sh In Array(Sheets(1), Sheets(2))
sh.Unprotect Password:="secret"
sh.Protect Password:="secret", UserInterFaceOnly:=True
Next
End Sub
Okay - so I have used this as a work around, but if anyone can give a more eloquent solution that would be great:
Dim ws As Worksheet
Set ws1 = ThisWorkbook.Worksheets(1)
Set ws2 = ThisWorkbook.Worksheets(2)
ws1.Unprotect ("2402")
ws2.Unprotect ("2402")
ws1.Cells.Copy Destination:=ws2.Cells
ws1.Protect ("2402")
ws2.Protect ("2402")
Like many other people, I want to be able to enable grouping and ungrouping with the little +/- buttons on a protected worksheet. Everyone seems to have succeeded with the same sort of code that protects the worksheet, enables outlining and then unprotects it again, which is great and it works except if I save the sheet and then re-open it again EnableOutlining is always set as False, and if the sheet is protected I cannot use the +/- buttons. Is there something else I am supposed to do to save this setting permanently, and not just for the duration of the session?
Here's the code I have been using:
Private Sub Workbook_Open()
MsgBox ActiveSheet.EnableOutlining
End Sub
Sub EnableOutliningWithProtection_AllSheets()
'PURPOSE: Allow Outline functionality during Protection in all Sheets
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
'(Except edited by me to include the Errorcatch)
Dim sht As Worksheet
On Error GoTo Errorcatch
'Loop through each Worksheet in ActiveWorkbook
For Each sht In ActiveWorkbook.Worksheets
'Password Protect Current Sheet
sht.Protect Password:="", UserInterfaceOnly:=True
'Enable Group Collapse/Expand Capabilities
sht.EnableOutlining = True
'Unprotect Sheet
sht.Unprotect ""
Next sht
Exit Sub
Errorcatch:
MsgBox Err.Description
End Sub
(I've got the Workbook_Open() bit to check if EnableOutlining was still True)
I've seen the 'protect UserInterfaceOnly and EnableOutlining' question, but I didn't think the results applied as the code was written for C#, and I'm not looking at protecting UserInterfaceOnly.
You can't save it permanently. You have to use the Open event to reset it when the workbook is opened.
Private Sub Workbook_Open()
EnableOutliningWithProtection_AllSheets
End Sub
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.