启动脚本的快捷方式(通过数据验证的Excel VBA下拉列表)

时间:2016-10-13 15:19:09

标签: excel-vba vba excel

实施了一个VBA脚本(不是我创建的),可以从输入EXCEL电子表格特定单元格的受控术语列表中选择多个单词。这基于数据验证列表。

问题如下:

  • 如果我在我不想使用该脚本的列中使用数据验证(这里需要数据验证,那么我可以在选择单元格时为用户添加注释;注释框不要帮助),覆盖术语或单元格删除不能正常工作。 所以我认为我可以将脚本改进为仅在单独的单元格/列中工作。我该怎么办?
  • 我希望有一个启动脚本的快捷方式,而不是双击(或额外)。

任何有关如何更改代码以解决这些问题的建议都将受到高度赞赏!

这是使用的代码:

   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

1 个答案:

答案 0 :(得分:0)

您提出了多个问题。
这不是您所有问题的完整答案。

我们假设您要将活动限制为仅更改 A B 列。

紧接着:

WHERE System.ItemPathDisplay LIKE '$c' AND CONTAINS('*$word*')

包括如下行:

strSep = ", "

修改此行以限制您感兴趣的列的活动。