簡體   English   中英

在Excel中隱藏選定范圍內的公式

[英]Hide formula in selected range in Excel

我正在使用以下代碼隱藏G11至G55中的公式。 但是它隱藏了所有公式並保護了工作表4。 如何只隱藏和保護G15:G55中的單元格? 謝謝。

Sub Loc()
    Dim c As Range
    Worksheets("sheet4").Range("G15:G55").Select

    For Each c In Selection
        If c.HasFormula = True Then
           c.Locked = True
           c.FormulaHidden = True
        End If
    Next c
    'Worksheets("sheet4").Range("G11:G55").Locked = True
    Worksheets("sheet4"").Protect Password:="111"
End Sub

可以修改以下代碼

Sub Locs()
    Dim c As Range, rng1, rng2 As Range
    Worksheets("Sheet4").Unprotect Password:="111"

    Cells.Locked = False
    Cells.FormulaHidden = False

    Set rng1 = Range("G15:G55")
    Set rng2 = Range("I15:I55")

    For Each c In rng1
        If c.HasFormula = True Then
            c.Locked = True
            c.FormulaHidden = True
        End If
    Next c

    For Each c In rng2
        If c.HasFormula = True Then
            c.Locked = True
            c.FormulaHidden = True
        End If
    Next c

    Worksheets("Sheet4").Protect Password:="111"
End Sub

考慮:

Sub Loc()
    Dim c As Range, rng As Range

    Worksheets("sheet4").Unprotect Password:="111"
        Cells.Locked = False
        Cells.FormulaHidden = False
        Set rng = Range("G15:G55")

        For Each c In rng
            If c.HasFormula = True Then
                c.Locked = True
                c.FormulaHidden = True
            End If
        Next c
    Worksheets("sheet4").Protect Password:="111"
End Sub

這僅鎖定感興趣的單元格並釋放其他單元格。 如果需要,可以將G15更改為G11

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM