【发布时间】:2016-10-18 19:31:37
【问题描述】:
实现了一个 VBA 脚本(不是我创建的),以便能够从输入到 EXCEL 电子表格特定单元格的受控术语列表中选择多个单词。这是基于数据验证列表。
问题如下:
- 如果我在不想使用脚本的列中使用数据验证(此处需要数据验证,因此我可以在选择单元格时为用户添加 cmets;注释框没有帮助),覆盖条款或单元格内删除无法正常工作。 所以我想我可以改进脚本只在单独的单元格/列中工作。我该怎么做?
- 我想要一个快捷方式来启动脚本,而不是(或附加)双击。
任何关于如何更改代码来解决这些问题的建议将不胜感激!
这是使用的代码:
Option Explicit
' ...
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim lType As Long
Dim strList As String
Application.EnableEvents = False
On Error Resume Next
lType = Target.Validation.Type
On Error GoTo exitHandler
If lType = 3 Then
'if the cell contains a data validation list
Cancel = True
strList = Target.Validation.Formula1
strList = Right(strList, Len(strList) - 1)
strDVList = strList
frmDVList.Show
End If
exitHandler:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDV As Range
Dim oldVal As String
Dim newVal As String
Dim strSep As String
strSep = ", "
Application.EnableEvents = False
On Error Resume Next
If Target.Count > 1 Then GoTo exitHandler
Set rngDV = Cells.SpecialCells(xlCellTypeAllValidation)
On Error GoTo exitHandler
If rngDV Is Nothing Then GoTo exitHandler
If Intersect(Target, rngDV) Is Nothing Then
'do nothing
Else
newVal = Target.Value
Application.Undo
oldVal = Target.Value
Target.Value = newVal
If newVal = "" Then
'do nothing
Else
If oldVal = "" Then
Target.Value = newVal
Else
Target.Value = oldVal & strSep & newVal
End If
End If
End If
exitHandler:
Application.EnableEvents = True
End Sub
【问题讨论】: