[英]Excel VBA prevent deletion of cells but allow edit
I have made a spreadsheet which a user can enter a postcode and a quantity into 2 cells and I have other cells doing calculations and displaying the results. 我制作了一个电子表格,用户可以将邮政编码和数量输入2个单元格,我还有其他单元格进行计算并显示结果。
I have added some VBA to prevent anyone from deleting rows and columns but I would like to prevent deletion of any cell within a range but also allow a user to make changes to certain cells but also prevent editing of cells with formula in there. 我添加了一些VBA以防止任何人删除行和列,但我想防止删除范围内的任何单元格,但也允许用户对某些单元格进行更改,但也阻止编辑具有公式的单元格。
In cell E4
, the user can enter a postcode. 在单元格E4
,用户可以输入邮政编码。 In E6
, the user can enter a quantity. 在E6
,用户可以输入数量。 These can be edited but not deleted. 这些可以编辑但不能删除。 E8:E9
and E11:E14
are all drop down lists (validation) which hold data from lists. E8:E9
和E11:E14
都是下拉列表(验证),用于保存列表中的数据。 These can be changed using the drop down but not deleted. 这些可以使用下拉菜单更改,但不能删除。
L10:L14
, L16
, L23:L27
, L29
, L30:L33
can all have their data edited but not deleted. L10:L14
, L16
, L23:L27
, L29
, L30:L33
都可以编辑数据但不能删除。
What would the VBA for this look like? 这个VBA会是什么样子? I guess it would use the Worksheet_Change() event
. 我想它会使用Worksheet_Change() event
。
Is this what you are trying? 这是你在尝试什么? Users can edit cell E4
and E6
but they cannot leave it empty. 用户可以编辑单元格E4
和E6
但不能将其留空。 I am also assuming that the cell are not empty before hand. 我也假设手前的细胞不是空的。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
If Not Intersect(Target, Range("E4")) Is Nothing Then
If Len(Trim(Range("E4").Value)) = 0 Then Application.Undo
ElseIf Not Intersect(Target, Range("E6")) Is Nothing Then
If Len(Trim(Range("E6").Value)) = 0 Then Application.Undo
End If
LetsContinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
FOLLOWUP 跟进
Thanks that is what i want to do. 谢谢,这就是我想要做的。 What about the other ranges? 其他范围怎么样? Is it just a case of loads of IF THEN or can we use a CASE and loop through? 它只是IF的负载情况,还是我们可以使用CASE并循环? – AdRock 2 mins ago - AdRock 2分钟前
Add/Delete cell addresses from below as applicable. 根据情况添加/删除下面的单元格地址。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
If Not Intersect(Target, Range("E4,E6,E8:E9,E11:E14,L10:L14,L16,L23:L27,L29,L30:L33")) Is Nothing Then
If Len(Trim(Target.Value)) = 0 Then Application.Undo
End If
LetsContinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume LetsContinue
End Sub
You are partly right but Worksheet_Change() is triggered after the change, so after the deletion. 部分正确,但更改后会触发Worksheet_Change(),因此删除后。 What I'd do is to have a hidden sheet to store the values entered by the user and then you can check in Worksheet_Change() whether the new value is empty (deleted) or not. 我要做的是有一个隐藏的工作表来存储用户输入的值,然后你可以在Worksheet_Change()中检查新值是否为空(删除)。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$4" Then
' check the previous value on the hidden sheet here, if changed, then save it, if empty, then restore it
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.