是否可以为组合框中的项目提供 ID 或密钥?

Is it possible to give an item in a combobox an ID or key?

我创建了一个用户窗体,其中有一个组合框和一个文本框。我的目标是在组合框中单击一个元素后,它会显示键、ID 或分配的任何内容。

这是我的来历:

 Private Sub UserForm_Initialize()
  With ComboBox1
     .AddItem "Item1", A
     .AddItem "Item2", B
     .AddItem "Item3", C
  End With
End Sub


  Private Sub ComboBox1_Change()
    Me.TextBox1.Value = Me.ComboBox1.ListIndex
  End Sub

此代码有效,但我得到的不是字母,而是从 0 开始的数值。我知道这是由于 listindex 属性。但是必须有一种方法可以正确获取字母,这样如果我 select ComboBox 中的 Item1 应该弹出 TextBox 中的字母 A,如果 select Item2 那么 B 应该弹出等等。

像下面这样定义你的ListValues

Option Explicit

Private Sub UserForm_Initialize()
    Dim ListValues(2, 1) As Variant
    ListValues(0, 0) = "Item1"
    ListValues(0, 1) = "A"
    
    ListValues(1, 0) = "Item2"
    ListValues(1, 1) = "B"
    
    ListValues(2, 0) = "Item3"
    ListValues(2, 1) = "C"
    
    With ComboBox1
        .List = ListValues
    End With
End Sub


Private Sub ComboBox1_Change()
    Me.TextBox1.Value = Me.ComboBox1.List(Me.ComboBox1.ListIndex, 1)
End Sub