简体   繁体   English

锁定/解锁单元VBA

[英]Lock/unlock cells VBA

I have been looking for a code to lock all the cells in a sheet but one and I found this code: 我一直在寻找一种代码来锁定工作表中的所有单元格,但是一个却发现了以下代码:

Worksheets("SW").Range("D2").Locked = False
Worksheets("SW").Protect UserInterfaceOnly:=True

it really worked fine until I saved it and closed/opened the file again, then any macros I had were run. 在我保存它并再次关闭/打开文件之前,它确实运行良好,然后运行了我所有的宏。

I guess it is because in that sheet I have different macros and also I have the macro that runs when a cell is changed: 我猜是因为在该工作表中我有不同的宏,而且我还有在更改单元格时运行的宏:

Private Sub Worksheet_Change(ByVal Target As Range)

Do you have any idea how to make it work? 您有任何想法如何使其工作吗?

One more thing, this workbook it will be shared, so I don't know that will affect the macros. 再说一遍,这个工作簿将被共享,所以我不知道这会影响宏。

sorry maybe I was too ambiguous. 对不起,也许我太模棱两可了。

Yes I want to prevent others to change de value of the cells. 是的,我想防止其他人更改单元格的值。

I want that just one cell could be changed so all the others are changing with the macros. 我希望只更改一个单元格,以便所有其他单元格都随着宏而更改。

Thanks again for all your help 再次感谢你的帮助

PS: yes Ahmad Al-Mutawa, I want to be impossible to chang the sheet even after the project being saved, closed and opened. PS:是的,艾哈迈德·穆塔瓦(Ahmad Al-Mutawa),即使在保存,关闭和打开项目之后,我也希望无法更改工作表。

If you need more info just tell me I'll try to be more specific. 如果您需要更多信息,请告诉我,我将尝试更加具体。

Does your file have any macros doing other operations than this lock unlock code you are trying to achieve? 您的文件中是否有任何宏在执行您尝试实现的此锁定解锁代码以外的其他操作?

If so I suggest you having a code in vba. 如果是这样,我建议您在vba中有一个代码。 Otherwise you can simply lock the sheets to prevent users from changing somethings or anytime at all without a code. 否则,您可以简单地锁定工作表以防止用户在没有代码的情况下或在任何时间进行任何更改。

do you think this could be better for you than a [code][1]? 您认为这比[code] [1]更好吗?

[ http://office.microsoft.com/en-us/excel-help/lock-or-unlock-specific-areas-of-a-protected-worksheet-HA010096837.aspx ][1] [ http://office.microsoft.com/zh-cn/excel-help/lock-or-unlock-specific-areas-of-a-protected-worksheet-HA010096837.aspx ] [1]

Out of curiosity: BY ANY CHANCE are you having an issue not knowing where and how to trigger this code piece? 出于好奇:您有任何可能不知道在何处以及如何触发此代码段的问题?

Edited code:- Your comment is not clear, nonethless I believe you want to use a macro. 编辑后的代码:-您的注释不清晰,除非我认为您想使用宏。 Assuming you want to protect/lock or unprotect/unlock Sheet1, please try the following. 假设您要保护/锁定或解锁/解锁Sheet1,请尝试以下操作。 :- :-

Add this code in Workbook open event in thisworkbook:- 在此工作簿的工作簿打开事件中添加以下代码:-

Private Sub Workbook_Open()
  Dim shtSheet As Worksheet
  Dim strPassword As String

    strPassword = "stack"
    Set shtSheet = Worksheets("Sheet1")
    shtSheet.Protect Password:=strPassword, UserInterfaceOnly:=True
End Sub

Next add the following in your module. 接下来,在您的模块中添加以下内容。 And call it after you finish running your macro. 在运行完宏后调用它。

 Sub ProtectSheet()
  Dim shtSheet As Worksheet
  Dim strPassword As String

     strPassword = "stack"
     Set shtSheet = Worksheets("Sheet1")
     shtSheet.Protect strPassword
 End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM