简体   繁体   English

系统锁定Excel工作表的代码,仅可用于1个电子表格(VB)

[英]Code to systematically lock excel sheets, only working on 1 spreadsheet (VB)

I'm basically trying to lock all but one sheet systematically (password driven) in excel. 我基本上是试图在Excel中系统地锁定除一张纸以外的所有文件(密码驱动)。 I've developed the following code, however, it only seems to be locking 1 sheet (Dec) of the many sheets. 我开发了以下代码,但是,它似乎只锁定了许多工作表中的1个工作表(12月)。 Any help would be HUGELY appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

Private Sub Workbook_BeforeClose(Cancel As Boolean)
 Dim ws As Worksheet
 Dim strPassWord As String
 strPassWord = "apple"

 Sheets("Jan").Select
 Sheets("Feb").Select
 Sheets("Mar").Select
 Sheets("April").Select
 Sheets("May").Select
 Sheets("June").Select
 Sheets("July").Select
 Sheets("Aug").Select
 Sheets("Sept").Select
 Sheets("Oct").Select
 Sheets("Nov").Select
 Sheets("Dec").Select
 ActiveSheet.Protect Password:=strPassWord, DrawingObjects:=True, Contents:=True, Scenarios:=True
 ActiveSheet.EnableSelection = xlNoSelection

End Sub

Private Sub Workbook_Open()
 Dim ws As Worksheet
 Dim strPassWord As String
 strPassWord = InputBox(Prompt:="Password", _
  Title:="Enter Password", Default:="User Password")

 If strPassWord = "apple" Then

  Sheets("Jan").Select
  Sheets("Feb").Select
  Sheets("Mar").Select
  Sheets("April").Select
  Sheets("May").Select
  Sheets("June").Select
  Sheets("July").Select
  Sheets("Aug").Select
  Sheets("Sept").Select
  Sheets("Oct").Select
  Sheets("Nov").Select
  Sheets("Dec").Select
  ActiveSheet.Unprotect Password:=strPassWord
 Else
  MsgBox ("Password Incorrect")
 End If
End Sub

Why are you writing code to do this when it is already built into Excel (See "Protect Sheet")? 当Excel中已经内置了代码时,为什么还要编写代码来做到这一点(请参阅“保护表”)? Re-inventing the wheel is generally not a good idea, doubly so for security, triply so when you are hard coding the password in the code which would easily be discovered. 重新发明轮子通常不是一个好主意,为了安全起见,这是双重的,三倍,因此当您用容易发现的密码对密码进行硬编码时。

To answer your core question however. 但是要回答您的核心问题。 ActiveSheet only refers to the sheet on top, not every one that you call select on. ActiveSheet仅引用顶部的工作表,而不是您调用的每个工作表。

You're going to need to select the sheet then password protect it. 您将需要选择工作表,然后对其进行密码保护。 The select function changes the ActiveSheet. 选择功能更改ActiveSheet。 It doesn't add to the selection. 它不会添加到选择中。 This would best be accomplished by moving the lock code to a function, then calling the function after selecting each sheet. 最好通过将锁定代码移至某个函数,然后在选择每张纸后调用该函数来实现。

Unlocking the sheet would need a similar tweak. 解锁工作表将需要进行类似的调整。

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ws As Worksheet
    Dim strPassWord As String
    strPassWord = "apple"

    Sheets("Jan").Select
    Call LockSheet
    Sheets("Feb").Select
    Call LockSheet
    Sheets("Mar").Select
    Call LockSheet
    Sheets("April").Select
    Call LockSheet
    Sheets("May").Select
    Call LockSheet
    Sheets("June").Select
    Call LockSheet
    Sheets("July").Select
    Call LockSheet
    Sheets("Aug").Select
    Call LockSheet
    Sheets("Sept").Select
    Call LockSheet
    Sheets("Oct").Select
    Call LockSheet
    Sheets("Nov").Select
    Call LockSheet
    Sheets("Dec").Select
    Call LockSheet

End Sub

Private Sub LockSheet

    ActiveSheet.Protect Password:=strPassWord, DrawingObjects:=True, Contents:=True, Scenarios:=True
    ActiveSheet.EnableSelection = xlNoSelection
End Sub

ActiveSheet can't be used this way as far as I know. 据我所知,ActiveSheet无法以这种方式使用。 Per the documentation: 根据文档:

The active sheet (the sheet on top); 活动工作表(位于工作表顶部); a null reference (Nothing in Visual Basic) if no sheet is active. 如果没有工作表处于活动状态,则返回null引用(在Visual Basic中为Nothing)。

It will only lock the last sheet you select. 它只会锁定您选择的最后一张纸。 Instead you would be better off having code like this: 相反,您最好拥有这样的代码:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim ws As Worksheet
    Dim strPassWord As String

    strPassWord = "apple"

    Call LockSheet(Sheets("Sheet1"))
    Call LockSheet(Sheets("Sheet2"))
    Call LockSheet(Sheets("Sheet3"))

End Sub

Private Sub Workbook_Open()
    Dim ws As Worksheet
    Dim strPassWord As String
    strPassWord = InputBox(Prompt:="Password", _
                            Title:="Enter Password", Default:="User Password")

    If strPassWord = "apple" Then
        Call UnlockSheet(Sheets("Sheet1"), strPassWord)
        Call UnlockSheet(Sheets("Sheet2"), strPassWord)
        Call UnlockSheet(Sheets("Sheet3"), strPassWord)
    Else
        MsgBox ("Password Incorrect")
    End If

End Sub

Private Sub LockSheet(sheet As Worksheet)
    sheet.Protect password:=strPassWord, DrawingObjects:=True, Contents:=True, Scenarios:=True
    sheet.EnableSelection = xlNoSelection
End Sub

Private Sub UnlockSheet(sheet As Worksheet, strPassWord As String)
    sheet.Unprotect password:=strPassWord
End Sub

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

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