在另一个单元格中输入后锁定的单元格 [英] a cell locked after input in another cell
本文介绍了在另一个单元格中输入后锁定的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在Excel中使用VB代码,如下所示:
I need VB code to use in Excel as below:
如果i在单元格A1中输入,则单元格B1被锁定,如果我在单元格B1中输入,则单元格A1为锁定,如果两个单元格都没有值,则不会被锁定。
If i input in cell A1 the cell B1 is locked and if I input in cell B1 the cell A1 is locked if both cell no value they will not be locked.
请帮帮我。
谢谢
推荐答案
首先执行此操作:
Do this first:
- 选择A1:B1,以及用户应该能够使用的所有其他单元格要编辑。
- 按Ctrl + 1激活"设置单元格"对话框。
- 激活"保护"选项卡。
- 清除"锁定"复选框。
- 单击"确定"。
- 激活功能区的"审阅"选项卡。
- 单击"保护表"。
- 如果需要,请指定密码,然后单击"确定"。
现在代码。
- 对 - 单击工作表选项卡。
- 从上下文菜单中选择"查看代码"。
- 将以下代码复制到工作表模块中:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("A1"), Target) Is Nothing Then
Application.EnableEvents = False
Me.Unprotect ' Password:="secret"
Range("B1").Locked = (Range("A1").Value <> "")
Me.Protect ' Password:="secret"
Application.EnableEvents = True
End If
If Not Intersect(Range("B1"), Target) Is Nothing Then
Application.EnableEvents = False
Me.Unprotect ' Password:="secret"
Range("A1").Locked = (Range("B1").Value <> "")
Me.Protect ' Password:="secret"
Application.EnableEvents = True
End If
End Sub
切换回Excel,不要忘记以宏启用格式(.xlsm,.xlsb或.xls)保存工作簿。
Switch back to Excel, and don't forget to save the workbook in a macro-enabled format (.xlsm, .xlsb or .xls).
这篇关于在另一个单元格中输入后锁定的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文