在VBA中,如何编写引用工作簿中所有工作表的数组

问题描述 投票:0回答:1

我正在使用电子表格大师的代码来批量保护/取消保护我的工作表。该文件由我更新并由其他团队成员使用。我有一些单元格可以编辑,而其他单元格在工作表受到保护时则不能编辑。

此表每年都会更换为新的表名称。有没有一种方法可以编写它,以便数组包含特定中的所有工作表,而不是列出数组中的每个工作表名称(“Sheet1”、“Sheet2”、“Sheet3”等)并每年重写此宏我已打开并尝试运行宏的文件?我还想确保它不会在我打开的任何其他文件上运行。我特别指的是所有 Dim 之后的输入 SheetArray。

'PURPOSE: Add/Remove password protection to a list of tab names
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault**your text**

Dim SheetArray As Variant
Dim sht As Worksheet
Dim Password As String
Dim NotFoundList As String
Dim WrongPasswordList As String
Dim ProtectionStateDetermined As Boolean
Dim UnprotectSheet As Boolean
Dim x As Long

'INPUTS
  SheetArray = Array("Sheet1", "Sheet2", "Sheet4")
  Password = "Password123"

'Loop through each sheet name
  For x = LBound(SheetArray) To UBound(SheetArray)
    'Store Sheet Object to Variable
      On Error Resume Next
      Set sht = Nothing
      Set sht = ActiveWorkbook.Sheets(SheetArray(x))
      On Error GoTo 0
    
    'Was Sheet found in Activeworkbook?
      If Not sht Is Nothing Then
        'Determine if we need to protect or unprotect these sheets (based off of first instance)
          If ProtectionStateDetermined = False And (sht.ProtectContents Or sht.ProtectDrawingObjects Or sht.ProtectScenarios) Then UnprotectSheet = True
          ProtectionStateDetermined = True
    
        'Lock/Unlock worksheet
          If UnprotectSheet = True Then
            On Error Resume Next
            sht.Unprotect Password
            If sht.ProtectContents = True Then WrongPasswordList = WrongPasswordList & "• " & SheetArray(x) & Chr(10)
            On Error GoTo 0
          Else
            sht.Protect Password
          End If
        
      Else
        'Store a list of sheets not found in the ActiveWorkbook
          NotFoundList = NotFoundList & "• " & SheetArray(x) & Chr(10)
      End If
  Next x

'Report what was done to the worksheets
  If UnprotectSheet = True Then
    MsgBox "Sheets Unprotected!"
  Else
    MsgBox "Sheets Protected!"
  End If

'Report any sheet names that were not found (if applicable)
  If NotFoundList <> "" Then
    MsgBox "The following Worksheets were not found in your Excel file:" & Chr(10) & Chr(10) & Trim(NotFoundList)
  End If

'Report any sheets that cound not be unprotected (if applicable)
  If WrongPasswordList <> "" Then
    MsgBox "The following Worksheets were unable to be unprotected:" & Chr(10) & Chr(10) & Trim(WrongPasswordList)
  End If

End Sub
arrays excel vba password-protection
1个回答
0
投票
  • 迭代
    ThisWorkbook.Worksheets
    处理每张纸
'Loop through each sheet name
  ' For x = LBound(SheetArray) To UBound(SheetArray)
  For Each sht in ThisWorkbook.Worksheets
    'Store Sheet Object to Variable
      ' On Error Resume Next
      ' Set sht = Nothing
      ' Set sht = ActiveWorkbook.Sheets(SheetArray(x))
      ' On Error GoTo 0
    
    ' 'Was Sheet found in Activeworkbook?
    '   If Not sht Is Nothing Then
        'Determine if we need to protect or unprotect these sheets (based off of first instance)
          If ProtectionStateDetermined = False And (sht.ProtectContents Or sht.ProtectDrawingObjects Or sht.ProtectScenarios) Then UnprotectSheet = True
          ProtectionStateDetermined = True
    
        'Lock/Unlock worksheet
          If UnprotectSheet = True Then
            On Error Resume Next
            sht.Unprotect Password
            If sht.ProtectContents = True Then WrongPasswordList = WrongPasswordList & "• " & SheetArray(x) & Chr(10)
            On Error GoTo 0
          Else
            sht.Protect Password
          End If
        
      ' Else
      '   'Store a list of sheets not found in the ActiveWorkbook
      '     NotFoundList = NotFoundList & "• " & SheetArray(x) & Chr(10)
      ' End If
  Next x
© www.soinside.com 2019 - 2024. All rights reserved.