Get position (in number) of selected item in dropdown list

南笙酒味 提交于 2020-01-09 11:17:09

问题


In a dropdown list I have a few items. Can I, when I select an item, get the position of that item in the list as a number?


回答1:


If you are using a list or combo box, ListIndex would seem to be what you are after.

VB Help for ListIndex property: Returns or sets the index number of the currently selected item in a list box or combo box. Read/write Long. Remarks. You cannot use this property with multiselect list boxes.

If nothing is selected, ListIndex's value is -1. If memory serves, it is a zero based index.

ListIndex cannot be set at design time so it is not listed in the properties window.

When entering your code, type the list box name then dot and the editor displays all the available properties. Scroll down the list, note any that look interesting, then look them up.




回答2:


If you are looking for the index of a Data Validation list, this is what I'd do:

Put the following code in the ThisWorkbook module:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Dim ValidationIndex As Long
Dim rngTest As Excel.Range

'assumes the data validation is in a cell named "rngTest"
On Error Resume Next
Set rngTest = Sh.Range("rngTest")
If rngTest Is Nothing Then
    Exit Sub
End If
On Error GoTo 0

If Not Intersect(ActiveCell, Sh.Range("rngTest")) Is Nothing Then
    ValidationIndex = GetValidationIndex
    MsgBox ValidationIndex
End If
End Sub

Put this function in the ThisWorkbook module also, or else in any regular module:

Function GetValidationIndex() As Long
'returns a 1-based index
Dim rngTest As Excel.Range
Dim varValidationString As Variant
Dim ErrNumber As Long
Dim i As Long

With ActiveCell.Validation
    If .Type = xlValidateList Then    '3
        On Error Resume Next
        Set rngTest = ActiveCell.Parent.Range(.Formula1)
        'I do this goofy thing with ErrNumber to keep my indenting and flow pretty
        ErrNumber = Err.Number
        On Error GoTo 0
        'if the Validation is defined as a range
        If ErrNumber = 0 Then
            GetValidationIndex = Application.WorksheetFunction.Match(ActiveCell.Value2, rngTest, 0)
            Exit Function
        'if the validation is defined by comma-separated values
        Else
            varValidationString = Split(.Formula1, ",")
            For i = LBound(varValidationString) To UBound(varValidationString)
                If varValidationString(i) = ActiveCell.Value2 Then
                    GetValidationIndex = i + 1
                    Exit Function
                End If
            Next i
        End If
    End If
End With
End Function



回答3:


I think it is not necessary to use a function. You can get it by using only Match function, like in above Doug's answer.

Dim GetValidationIndex as Integer
Dim rngTest as Range      
' Get the validation list
With ActiveCell.Validation
   Set rngTest = ActiveCell.Parent.Range(.Formula1)
end with

GetValidationIndex = Application.WorksheetFunction.Match(ActiveCell.Value2, rngTest, 0)


来源:https://stackoverflow.com/questions/8458071/get-position-in-number-of-selected-item-in-dropdown-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!