Excel VBA解锁单元格以运行宏,然后再次锁定

时间:2016-09-29 19:12:40

标签: excel vba excel-vba

我的工作簿中的某些单元格已被锁定,但遗憾的是这会阻止宏运行。因此,我修改了位于http://excel-macro.tutorialhorizon.com/excel-vba-range-lockunlock-specific-cells/的宏以解锁工作表,运行宏然后重新锁定。我只是想发布一个解决方案。

Sub sumit2()
  'Unlocks cells A1:C5
 Dim mainworkBook As Workbook

 Set mainworkBook = ActiveWorkbook

   ActiveSheet.Unprotect Password:="xx"


   mainworkBook.Sheets("Sheet1").Range("A1:C5").Locked = False

  Call FillCell

  End Sub
Sub FillCell()

  Dim mainworkBook As Workbook

  Set mainworkBook = ActiveWorkbook

  mainworkBook.Sheets("Sheet1").Range("A1:C5").Value = "Locked"

Call sumit
End Sub
Sub sumit()
'Protects cells A1:C5
  Dim mainworkBook As Workbook

  Set mainworkBook = ActiveWorkbook

    ActiveSheet.Unprotect Password:="xx"


   mainworkBook.Sheets("Sheet1").Range("A1:C5").Locked = True

   ActiveSheet.Protect Password:="xx"


 End Sub

0 个答案:

没有答案