Excel - 如何使用VBA有条件地锁定和解锁特定单元格。

时间:2015-05-26 16:19:41

标签: excel vba excel-vba locking conditional-formatting

标题说明了一切。有什么见解吗?这段代码绝对不起作用:

/**
 * Flattens a multi demensional array into a one dimensional
 * to be compatible with hidden html fields.
 *
 * @param array $array
 *  Array in the form:
 *  array(
 *    'a' => array(
 *      'b' => '1'
 *    )
 *  )
 *
 * @return array
 *  Array in the form:
 *  array(
 *    'a[b]' => 1,
 *  )
 */
function flatten_array($array) {
  // Continue until $array is a one-dimensional array.
  $continue = TRUE;
  while ($continue) {
    $continue = FALSE;

    // Walk through top and second level of $array and move 
    // all values in the second level up one level.
    foreach ($array as $key => $value) {
      if (is_array($value)) {
        // Second level found, therefore continue.
        $continue = TRUE;

        // Move each value a level up.
        foreach ($value as $child_key => $child_value) {
          $array[$key . '[' . $child_key . ']'] = $child_value;
        }

        // Remove second level array from top level.
        unset($array[$key]);
      }
    }
  }

  return $array;
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

如果单元格 D14 包含并且运行了宏,则单元格将被解锁。
如果单元格 D14 不包含并且运行了宏,则该单元格将被锁定。

Sub MrFreeze()

   Dim cCell As Range
   Dim wksInput As Worksheet

   Set wksInput = Worksheets("Input")
   Set cCell = wksInput.Range("D14")

   If cCell.Value = "Yes" Then
      ActiveSheet.Unprotect Password = "password"
         cCell.Locked = False
      ActiveSheet.Unprotect Password = "password"
   Else
      ActiveSheet.Unprotect Password = "password"
         cCell.Locked = True
      ActiveSheet.Protect Password = "password"
   End If

End Sub