Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can modify a sheet via code by taking these actions</p> <ul> <li>Unprotect</li> <li>Modify</li> <li>Protect</li> </ul> <p>In code this would be:</p> <pre><code>Sub UnProtect_Modify_Protect() ThisWorkbook.Worksheets("Sheet1").Unprotect Password:="Password" 'Unprotect ThisWorkbook.ActiveSheet.Range("A1").FormulaR1C1 = "Changed" 'Modify ThisWorkbook.Worksheets("Sheet1").Protect Password:="Password" 'Protect End Sub </code></pre> <p>The <strong>weakness</strong> of this method is that if the code is interrupted and error handling does not capture it, the worksheet could be left in an unprotected state.</p> <p>The code could be <strong>improved</strong> by taking these actions</p> <ul> <li>Re-protect</li> <li>Modify</li> </ul> <p>The code to do this would be:</p> <pre><code>Sub Re-Protect_Modify() ThisWorkbook.Worksheets("Sheet1").Protect Password:="Password", _ UserInterfaceOnly:=True 'Protect, even if already protected ThisWorkbook.ActiveSheet.Range("A1").FormulaR1C1 = "Changed" 'Modify End Sub </code></pre> <p>This code renews the protection on the worksheet, but with the ‘UserInterfaceOnly’ set to true. This allows VBA code to modify the worksheet, while keeping the worksheet protected from user input via the UI, even if execution is interrupted.</p> <p>This setting is <strong>lost</strong> when the workbook is closed and re-opened. The worksheet protection is still maintained.</p> <p>So the 'Re-protection' code needs to be included at the start of any procedure that attempts to modify the worksheet or can just be run once when the workbook is opened.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload