锁定特定工作表中的单元格

Lock cells in specific worksheets

我正在尝试锁定工作簿中特定工作表内的范围。我使用的代码只锁定第一个工作表的单元格,不会循环到下一个锁定范围的单元格。

Sub lockpivots()

Dim sh As Worksheet

For Each sh In Sheets

Select Case sh.Name
Case Is = "2) Review Charts-Management", "3) Review Charts-Functional"

    Selection.Locked = False
    Selection.FormulaHidden = False
    range("b2:C9").Select
    ActiveSheet.Protect Password:="IDR"
    
    End Select
    
    Next sh
    
End Sub

不要使用 Selection.SelectActiveSheet,而是参考 sh:

For Each sh In ThiseWorkbook.Worksheets 
    Select Case sh.Name
        Case "2) Review Charts-Management", "3) Review Charts-Functional"
            With sh
                .Range("B2:C9").Locked = False
                .Range("B2:C9").FormulaHidden = False
                .Protect Password:="IDR"
            End With
    End Select
Next sh

锁定范围内的单元格

Option Explicit

Sub LockPivots()

    Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
    
        Select Case ws.Name
        Case "2) Review Charts-Management", "3) Review Charts-Functional"
            ws.UnProtect Password:="IDR"
            With ws.Range("B2:C9")
                .Locked = False
                .FormulaHidden = False
            End With
            ws.Protect Password:="IDR"
        Case Else ' do nothing
        End Select

    Next ws
    
End Sub