首页 > 解决方案 > VBA 数据验证 - 选择问题

问题描述

提前,对不起我的英语不好。我一直在为此苦苦挣扎,我需要一些帮助。我正在使用来自工作表代码的验证代码。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 If Not Intersect(ActiveCell, Range("M:M")) Is Nothing Then

 'I've some code here just to filter the list to display

 With Target.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
    xlBetween, Formula1:=blah
    .IgnoreBlank = True
    .InCellDropdown = True
    .InputTitle = ""
    .ErrorTitle = ""
    .InputMessage = ""
    .ErrorMessage = ""
    .ShowInput = True
    .ShowError = False
End With
End If

问题是每当我在工作表中进行选择时,例如:从 K 到 T,我“通过”M,因此代码/验证将应用于整个选择。我只想在单击 M2、M3 等时申请。

我将不胜感激任何帮助!

问候

标签: excelvba

解决方案


首先,你被给予Target,你应该利用它而不是ActiveCell.

其次,跳过“M”行中的任何目标。为了计算所有可能性,您必须检查

  • 要更改的范围只有 1 列宽
  • 范围列是“M”

这可以通过以下方式完成:

If Not (Target.Columns.Count = 1 And Target.Column = 13) Then
    MsgBox "Co-cooo"
End If

推荐阅读