=MATCH(A1, B1:B5, 0)
Sub GetDropdownIndex() Dim dropdown As DropDown Dim selectedIndex As Long ' 假設下拉選單在 Sheet1 的 A1 單元格 Set dropdown = Sheet1.DropDowns("Drop Down 1") ' 請確認下拉選單的名稱 selectedIndex = dropdown.ListIndex ' 如果有選取項目,顯示索引;否則顯示未選取 If selectedIndex > 0 Then MsgBox "目前選到的選項是第 " & selectedIndex & " 個" Else MsgBox "未選取任何選項" End If End Sub
Sub GetDataValidationIndex() Dim cell As Range Dim sourceRange As Range Dim selectedValue As String Dim index As Long ' 假設下拉選單在 Sheet1 的 A1 單元格 Set cell = Sheet1.Range("A1") selectedValue = cell.Value ' 假設下拉選單的來源範圍是 B1:B5 Set sourceRange = Sheet1.Range("B1:B5") ' 找出選取值在來源範圍中的位置 index = Application.Match(selectedValue, sourceRange, 0) ' 顯示結果 If Not IsError(index) Then MsgBox "目前選到的選項是第 " & index & " 個" Else MsgBox "未選取任何選項或值不在來源範圍中" End If End Sub