【问题标题】:shortcut to start script (Excel VBA Drop Down List via Data Validation)启动脚本的快捷方式(通过数据验证的 Excel VBA 下拉列表)
【发布时间】: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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您问了不止一个问题。
    这不是您所有问题的完整答案。

    假设您希望将活动限制为仅更改列 AB

    紧接着:

    strSep = ", "
    

    包括如下一行:

    If Intersect(Target,Range("A:B")) is Nothing Then Exit Sub
    

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

    【讨论】:

    • 谢谢,工作正常,将第二个 if 更改为 is
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2017-05-10
    • 2020-04-01
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多