Hide formula in selected range in Excel
<p>I am using the following code to hide formulas in G11 to G55. But it hides all formulas and protects the sheet4. How can I only hide and protect cell in G15:G55? Thank you.   </p>

<pre><code>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
</code></pre>

<p>Can following code be modified </p>

<pre><code>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
</code></pre>