我目前正在使用两段vba代码。我试图将他们结合在一起以达到以下目的。
第一个代码
我的代码允许用户双击单元格,然后为单元格加盖时间戳,然后锁定相关单元格。哪个工作正常。但是在某些情况下,用户必须键入NA 并运行不同的代码(第二代码)。
result_list = []
for word in words:
dictionary_to_add = GLOBAL.copy()
dictionary_to_add["second"] = word
result_list.append(dictionary_to_add)
用户类型NA
时的第二个代码Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
With Target
If .Column = 4 Then
Select Case .Row
Case 20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65
Sheets("Sheet3").Protect Password:="Test", userinterfaceonly:=True
.Value2 = "Prepared By" & " " & Environ("Username")
.Value2 = .Value2 & " " & Format(Now, "yyyy-MM-dd hh:mm:ss")
.Locked = True
End Select
End If
End With
End Sub
克服的问题
一旦用户双击单元格并锁定,则无法将该单元格更改为NA。一旦单元格被锁定,NA应该是用户应该能够键入的唯一值。
问题
有没有办法让我只允许用户在单元格锁定后输入NA。因此,用户只有两个选项可以输入 na或双击
答案 0 :(得分:1)
显然,在锁定单元格之前,您无法输入任何内容。添加数据验证仅允许“NA”而不是锁定单元格。像这样:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
With Target
If .Column = 4 Then
Select Case .Row
Case 20, 24, 25, 27, 28, 30, 31, 32, 33, 34, 35, 37, 38, 40, 42, 43, 44, 54, 55, 56, 58, 59, 61, 62, 63, 64, 65
.Value2 = "Prepared By" & " " & Environ("Username") & " " & Format(Now, "yyyy-MM-dd hh:mm:ss")
With .Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="NA"
.IgnoreBlank = False
.InCellDropdown = False
.ErrorTitle = "Invalid input"
.ErrorMessage = "NA available only"
.ShowError = True
End With
End Select
End If
End With
Application.EnableEvents = True
End Sub
此方法假定工作表根本不受保护。