简体   繁体   English

保护工作表而不是工作簿

[英]Protect worksheet not workbook

I have an Excel spreadsheet that needs most of it's cells protected from editing. 我有一个Excel电子表格,它需要保护大多数单元格免受编辑。 I can't protect the sheet in the usual way because I work with groups (little + at the top to expand certain columns). 我无法以通常的方式保护工作表,因为我与小组一起工作(顶部的小号+用于扩展某些列)。

I found a VBA macro to protect my sheet, but noticed in Excel 2010 that I could simply "unprotect sheet" and modify everything, even though the whole workbook is still protected. 我找到了一个VBA宏来保护我的工作表,但在Excel 2010中注意到,即使整个工作簿仍受保护,我也可以“取消保护工作表”并修改所有内容。

This is the macro I use at "ThisWorkbook": 这是我在“ ThisWorkbook”上使用的宏:

Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect Password:="nopassword" 
If Range("C3").Value = "protect" Then 
    Range("C4:C65536").Locked = True 
Else
    Range("C4:C65536").Locked = False
End If
ActiveSheet.Protect Password:="fakepass" 
End Sub

Private Sub Workbook_Open()
Dim x As Long

For x = 1 To ActiveWorkbook.Sheets.Count
    With ActiveWorkbook.Sheets(x)
        .Protect UserInterfaceOnly:=True
        .EnableOutlining = True
    End With
Next

End Sub

How can I modify this code to work with Sheet 1? 如何修改此代码以与工作表1一起使用?

I'm aware it's not the safest form of protection but it's merely to prevent people modifying cells accidentally. 我知道这不是最安全的保护方式,而只是为了防止人们意外修改电池。

If you change: 如果您更改:

ActiveSheet.Protect Password:="fakepass" 

To: 至:

Worksheets("Sheet1").Protect Password:="fakepass"

It will apply to Sheet1 rather than the active sheet only. 它仅适用于Sheet1,而不是仅适用于活动表。

Or you could create a macro to protect all sheets, something like: 或者,您可以创建一个宏来保护所有工作表,例如:

Sub ProtectAll()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
ws.Protect Password:="fakepass", DrawingObjects:=True, Contents:=True, Scenarios:=True
Next ws

End Sub

And then call it into your main code? 然后调用它到您的主代码中?

ActiveSheet.Unprotect Password:="nopassword" Will only reference whatever sheet you're on. ActiveSheet.Unprotect Password:="nopassword"将仅引用您正在使用的任何工作表。

Sheets("Sheet1").Activate will set active sheet to sheet1, no matter what sheet is selected. Sheets("Sheet1").Activate会将活动工作表设置为sheet1,无论选择了哪个工作表。

Is that what you were after? 那是你的追求吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM