下拉列表选择的验证

问题描述 投票:0回答:1

我在每一列的每个单元格中都有下拉列表。如果我在过程1下为批次代码BOL选择说GR1,则可以将GR1用于该批次代码,但不能用于任何其他批次代码。

一旦我在除BOL之外的任何其他批处理代码中的任何其他过程中的其他任何地方选择GR1,我将收到一条错误消息,指出我无法选择它。

也只是为了清楚起见,下拉列表中的值已根据数组中的条件填充。

当前,我可以在整个工作表中选择相同的值。

enter image description here

arrays excel vba validation
1个回答
0
投票

这可能有用。

使用Worksheet_SelectionChange事件即时修改验证列表。

例如:

'myList and myRange are assumed to be named ranges referring
'to the full Validation List and the Range over which the
'Validation is to be applied.
'
'You can refer to them by other means, depending on your setup
'
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim col As Collection
  Dim V, W, I As Long

If Not Intersect(Target, [myRange]) Is Nothing Then
    Set col = New Collection
    With [myRange]
        For Each V In [myList] 'or however the validation list is set up
            If .Find(what:=V, after:=.Item(1), LookIn:=xlValues, _
                lookat:=xlWhole, MatchCase:=True) Is Nothing Then
                    col.Add V
            End If
        Next V

        'Add contents of target if in myList
        If Len(Target.Value) > 0 Then
            For Each V In [myList]
                If V = Target.Value Then
                    col.Add V
                    Exit For
                End If
            Next V
        End If
    End With

    ReDim W(1 To col.Count)
    I = 0
    For Each V In col
        I = I + 1
        W(I) = V
    Next V
   Target.Validation.Modify Formula1:=Join(W, ",")
End If

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.