如何检查单元格是否有列表框

时间:2017-03-18 15:59:54

标签: vba excel-vba excel

所以,如果某个人选择了一个与其关联的列表框的单元格,我该如何检测它?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
           'Check if Target cell has listbox?
End Sub

1 个答案:

答案 0 :(得分:1)

你可以使用这个功能:

Function HasValidation(rng As Range) As Boolean
    Dim validationType As Long

    validationType = -1
    On Error Resume Next
    validationType = rng.Validation.Type
    HasValidation = validationType >= 0
End Function

并在您的事件处理程序中利用它:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If HasValidation(Target) Then
        ... your code
    End If
End Sub